
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>smb.base &#8212; pysmb 1.2.8 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/sphinxdoc.css" />
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
    <script src="../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">pysmb 1.2.8 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">smb.base</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for smb.base</h1><div class="highlight"><pre>
<span></span>
<span class="kn">import</span> <span class="nn">logging</span><span class="o">,</span> <span class="nn">binascii</span><span class="o">,</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">hmac</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">.smb_constants</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">.smb2_constants</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">.smb_structs</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">.smb2_structs</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">.security_descriptors</span> <span class="kn">import</span> <span class="n">SecurityDescriptor</span>
<span class="kn">from</span> <span class="nn">nmb.base</span> <span class="kn">import</span> <span class="n">NMBSession</span>
<span class="kn">from</span> <span class="nn">.utils</span> <span class="kn">import</span> <span class="n">convertFILETIMEtoEpoch</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">ntlm</span><span class="p">,</span> <span class="n">securityblob</span>
<span class="kn">from</span> <span class="nn">.strategy</span> <span class="kn">import</span> <span class="n">DataFaultToleranceStrategy</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">hashlib</span>
    <span class="n">sha256</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">sha256</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">.utils</span> <span class="kn">import</span> <span class="n">sha256</span>


<div class="viewcode-block" id="NotReadyError"><a class="viewcode-back" href="../../api/smb_exceptions.html#smb.base.NotReadyError">[docs]</a><span class="k">class</span> <span class="nc">NotReadyError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Raised when SMB connection is not ready (i.e. not authenticated or authentication failed)&quot;&quot;&quot;</span>
    <span class="k">pass</span></div>

<div class="viewcode-block" id="NotConnectedError"><a class="viewcode-back" href="../../api/smb_exceptions.html#smb.base.NotConnectedError">[docs]</a><span class="k">class</span> <span class="nc">NotConnectedError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Raised when underlying SMB connection has been disconnected or not connected yet&quot;&quot;&quot;</span>
    <span class="k">pass</span></div>

<div class="viewcode-block" id="SMBTimeout"><a class="viewcode-back" href="../../api/smb_exceptions.html#smb.base.SMBTimeout">[docs]</a><span class="k">class</span> <span class="nc">SMBTimeout</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Raised when a timeout has occurred while waiting for a response or for a SMB/CIFS operation to complete.&quot;&quot;&quot;</span>
    <span class="k">pass</span></div>


<span class="k">class</span> <span class="nc">SMB</span><span class="p">(</span><span class="n">NMBSession</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class represents a &quot;connection&quot; to the remote SMB/CIFS server.</span>
<span class="sd">    It is not meant to be used directly in an application as it does not have any network transport implementations.</span>

<span class="sd">    For application use, please refer to</span>
<span class="sd">      - L{SMBProtocol.SMBProtocolFactory&lt;smb.SMBProtocol&gt;} if you are using Twisted framework</span>

<span class="sd">    In [MS-CIFS], this class will contain attributes of Client, Client.Connection and Client.Session abstract data models.</span>

<span class="sd">    References:</span>
<span class="sd">    ===========</span>
<span class="sd">      - [MS-CIFS]: 3.2.1</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;SMB.SMB&#39;</span><span class="p">)</span>

    <span class="n">SIGN_NEVER</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">SIGN_WHEN_SUPPORTED</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">SIGN_WHEN_REQUIRED</span> <span class="o">=</span> <span class="mi">2</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">my_name</span><span class="p">,</span> <span class="n">remote_name</span><span class="p">,</span> <span class="n">domain</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">use_ntlm_v2</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> <span class="n">sign_options</span> <span class="o">=</span> <span class="n">SIGN_WHEN_REQUIRED</span><span class="p">,</span> <span class="n">is_direct_tcp</span> <span class="o">=</span> <span class="kc">False</span><span class="p">):</span>
        <span class="n">NMBSession</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">my_name</span><span class="p">,</span> <span class="n">remote_name</span><span class="p">,</span> <span class="n">is_direct_tcp</span> <span class="o">=</span> <span class="n">is_direct_tcp</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_password</span> <span class="o">=</span> <span class="n">password</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">domain</span> <span class="o">=</span> <span class="n">domain</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sign_options</span> <span class="o">=</span> <span class="n">sign_options</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_direct_tcp</span> <span class="o">=</span> <span class="n">is_direct_tcp</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_ntlm_v2</span> <span class="o">=</span> <span class="n">use_ntlm_v2</span> <span class="c1">#: Similar to LMAuthenticationPolicy and NTAuthenticationPolicy as described in [MS-CIFS] 3.2.1.1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_using_smb2</span> <span class="o">=</span> <span class="kc">False</span>   <span class="c1">#: Are we communicating using SMB2 protocol? self.smb_message will be a SMB2Message instance if this flag is True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">async_requests</span> <span class="o">=</span> <span class="p">{</span> <span class="p">}</span>    <span class="c1">#: AsyncID mapped to _PendingRequest instance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span> <span class="o">=</span> <span class="p">{</span> <span class="p">}</span>  <span class="c1">#: MID mapped to _PendingRequest instance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span> <span class="o">=</span> <span class="p">{</span> <span class="p">}</span>   <span class="c1">#: Share name mapped to TID</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">next_rpc_call_id</span> <span class="o">=</span> <span class="mi">1</span>    <span class="c1">#: Next RPC callID value. Not used directly in SMB message. Usually encapsulated in sub-commands under SMB_COM_TRANSACTION or SMB_COM_TRANSACTION2 messages</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">has_negotiated</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="kc">False</span>           <span class="c1">#: True if the remote server accepts message signing. All outgoing messages will be signed. Simiar to IsSigningActive as described in [MS-CIFS] 3.2.1.2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">signing_session_key</span> <span class="o">=</span> <span class="kc">None</span>          <span class="c1">#: Session key for signing packets, if signing is active. Similar to SigningSessionKey as described in [MS-CIFS] 3.2.1.2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">signing_challenge_response</span> <span class="o">=</span> <span class="kc">None</span>   <span class="c1">#: Contains the challenge response for signing, if signing is active. Similar to SigningChallengeResponse as described in [MS-CIFS] 3.2.1.2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mid</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">next_signing_id</span> <span class="o">=</span> <span class="mi">2</span>     <span class="c1">#: Similar to ClientNextSendSequenceNumber as described in [MS-CIFS] 3.2.1.2</span>

        <span class="c1"># SMB1 and SMB2 attributes</span>
        <span class="c1"># Note that the interpretations of the values may differ between SMB1 and SMB2 protocols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">capabilities</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">=</span> <span class="mi">0</span>     <span class="c1">#: Initialized from the SecurityMode field of the SMB_COM_NEGOTIATE message</span>

        <span class="c1"># SMB1 attributes</span>
        <span class="c1"># Most of the following attributes will be initialized upon receipt of SMB_COM_NEGOTIATE message from server (via self._updateServerInfo_SMB1 method)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_plaintext_authentication</span> <span class="o">=</span> <span class="kc">False</span>  <span class="c1">#: Similar to PlaintextAuthenticationPolicy in in [MS-CIFS] 3.2.1.1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_raw_size</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_buffer_size</span> <span class="o">=</span> <span class="mi">0</span>   <span class="c1">#: Similar to MaxBufferSize as described in [MS-CIFS] 3.2.1.1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_mpx_count</span> <span class="o">=</span> <span class="mi">0</span>     <span class="c1">#: Similar to MaxMpxCount as described in [MS-CIFS] 3.2.1.1</span>

        <span class="c1"># SMB2 attributes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_read_size</span> <span class="o">=</span> <span class="mi">0</span>      <span class="c1">#: Similar to MaxReadSize as described in [MS-SMB2] 2.2.4</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_write_size</span> <span class="o">=</span> <span class="mi">0</span>     <span class="c1">#: Similar to MaxWriteSize as described in [MS-SMB2] 2.2.4</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_transact_size</span> <span class="o">=</span> <span class="mi">0</span>  <span class="c1">#: Similar to MaxTransactSize as described in [MS-SMB2] 2.2.4</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">session_id</span> <span class="o">=</span> <span class="mi">0</span>         <span class="c1">#: Similar to SessionID as described in [MS-SMB2] 2.2.4. This will be set in _updateState_SMB2 method</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_setupSMB1Methods</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication with remote machine &quot;</span><span class="si">%s</span><span class="s1">&quot; for user &quot;</span><span class="si">%s</span><span class="s1">&quot; will be using NTLM </span><span class="si">%s</span><span class="s1"> authentication (</span><span class="si">%s</span><span class="s1"> extended security)&#39;</span><span class="p">,</span>
                      <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span>
                      <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">use_ntlm_v2</span> <span class="ow">and</span> <span class="s1">&#39;v2&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">&#39;v1&#39;</span><span class="p">,</span>
                      <span class="p">(</span><span class="n">SUPPORT_EXTENDED_SECURITY</span> <span class="ow">and</span> <span class="s1">&#39;with&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">&#39;without&#39;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">password</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_password</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_password</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_password</span>

    <span class="c1">#</span>
    <span class="c1"># NMBSession Methods</span>
    <span class="c1">#</span>

    <span class="k">def</span> <span class="nf">onNMBSessionOK</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComNegotiateRequest</span><span class="p">()))</span>

    <span class="k">def</span> <span class="nf">onNMBSessionFailed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">onNMBSessionMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flags</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">i</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">SMB2ProtocolHeaderError</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Now switching over to SMB2 protocol communication&#39;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">is_using_smb2</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">mid</span> <span class="o">=</span> <span class="mi">0</span>  <span class="c1"># Must reset messageID counter, or else remote SMB2 server will disconnect</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_setupSMB2Methods</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_klassSMBMessage</span><span class="p">()</span>
                <span class="n">i</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

            <span class="n">next_message_offset</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_using_smb2</span><span class="p">:</span>
                <span class="n">next_message_offset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">next_command_offset</span>

            <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_using_smb2</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Received SMB message &quot;</span><span class="si">%s</span><span class="s1">&quot; (command:0x</span><span class="si">%2X</span><span class="s1"> flags:0x</span><span class="si">%02X</span><span class="s1"> flags2:0x</span><span class="si">%04X</span><span class="s1"> TID:</span><span class="si">%d</span><span class="s1"> UID:</span><span class="si">%d</span><span class="s1">)&#39;</span><span class="p">,</span>
                                   <span class="n">SMB_COMMAND_NAMES</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">command</span><span class="p">,</span> <span class="s1">&#39;&lt;unknown&gt;&#39;</span><span class="p">),</span>
                                   <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">command</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">flags</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">flags2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">uid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Received SMB2 message &quot;</span><span class="si">%s</span><span class="s1">&quot; (command:0x</span><span class="si">%04X</span><span class="s1"> flags:0x</span><span class="si">%04x</span><span class="s1">)&#39;</span><span class="p">,</span>
                                   <span class="n">SMB2_COMMAND_NAMES</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">command</span><span class="p">,</span> <span class="s1">&#39;&lt;unknown&gt;&#39;</span><span class="p">),</span>
                                   <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">command</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="o">.</span><span class="n">flags</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_updateState</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span><span class="p">):</span>
                    <span class="c1"># We need to create a new instance instead of calling reset() because the instance could be captured in the message history.</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">smb_message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_klassSMBMessage</span><span class="p">()</span>

            <span class="k">if</span> <span class="n">next_message_offset</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">next_message_offset</span><span class="p">:]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>

    <span class="c1">#</span>
    <span class="c1"># Public Methods for Overriding in Subclasses</span>
    <span class="c1">#</span>

    <span class="k">def</span> <span class="nf">onAuthOK</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">onAuthFailed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="c1">#</span>
    <span class="c1"># Protected Methods</span>
    <span class="c1">#</span>

    <span class="k">def</span> <span class="nf">_setupSMB1Methods</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_klassSMBMessage</span> <span class="o">=</span> <span class="n">SMBMessage</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_updateState</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_updateState_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_updateServerInfo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_updateServerInfo_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_handleNegotiateResponse</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handleNegotiateResponse_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_handleSessionChallenge</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handleSessionChallenge_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_listShares</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_listShares_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_listPath</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_listPath_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_listSnapshots</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_listSnapshots_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_getSecurity</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getSecurity_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_getAttributes</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getAttributes_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFile_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFileFromOffset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFileFromOffset_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_storeFile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_storeFile_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_storeFileFromOffset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_storeFileFromOffset_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resetFileAttributes</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resetFileAttributes_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_createDirectory</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_createDirectory_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_deleteDirectory</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_deleteDirectory_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_rename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_rename_SMB1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_echo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_echo_SMB1</span>

    <span class="k">def</span> <span class="nf">_setupSMB2Methods</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_klassSMBMessage</span> <span class="o">=</span> <span class="n">SMB2Message</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_updateState</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_updateState_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_updateServerInfo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_updateServerInfo_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_handleNegotiateResponse</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handleNegotiateResponse_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_handleSessionChallenge</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handleSessionChallenge_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_listShares</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_listShares_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_listPath</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_listPath_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_listSnapshots</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_listSnapshots_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_getSecurity</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getSecurity_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_getAttributes</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getAttributes_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFile_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFileFromOffset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFileFromOffset_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_storeFile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_storeFile_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_storeFileFromOffset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_storeFileFromOffset_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resetFileAttributes</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resetFileAttributes_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_createDirectory</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_createDirectory_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_deleteDirectory</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_deleteDirectory_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_rename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_rename_SMB2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_echo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_echo_SMB2</span>

    <span class="k">def</span> <span class="nf">_getNextRPCCallID</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">next_rpc_call_id</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">next_rpc_call_id</span>

    <span class="c1">#</span>
    <span class="c1"># SMB2 Methods Family</span>
    <span class="c1">#</span>

    <span class="k">def</span> <span class="nf">_sendSMBMessage_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">smb_message</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">mid</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">mid</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getNextMID_SMB2</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">command</span> <span class="o">!=</span> <span class="n">SMB2_COM_NEGOTIATE</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">session_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">session_id</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">SMB2_FLAGS_SIGNED</span>
            <span class="n">raw_data</span> <span class="o">=</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">signature</span> <span class="o">=</span> <span class="n">hmac</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">signing_session_key</span><span class="p">,</span> <span class="n">raw_data</span><span class="p">,</span> <span class="n">sha256</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">()[:</span><span class="mi">16</span><span class="p">]</span>

            <span class="n">smb_message</span><span class="o">.</span><span class="n">raw_data</span> <span class="o">=</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;MID is </span><span class="si">%d</span><span class="s1">. Signature is </span><span class="si">%s</span><span class="s1">. Total raw message is </span><span class="si">%d</span><span class="s1"> bytes&#39;</span><span class="p">,</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">smb_message</span><span class="o">.</span><span class="n">signature</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">smb_message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">raw_data</span> <span class="o">=</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sendNMBMessage</span><span class="p">(</span><span class="n">smb_message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_getNextMID_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mid</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mid</span>

    <span class="k">def</span> <span class="nf">_updateState_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">isReply</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">command</span> <span class="o">==</span> <span class="n">SMB2_COM_NEGOTIATE</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_negotiated</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;SMB2 dialect negotiation successful&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_updateServerInfo</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_handleNegotiateResponse</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="s1">&#39;Unknown status value (0x</span><span class="si">%08X</span><span class="s1">) in SMB2_COM_NEGOTIATE&#39;</span> <span class="o">%</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="p">,</span>
                                        <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">command</span> <span class="o">==</span> <span class="n">SMB2_COM_SESSION_SETUP</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">session_id</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">session_id</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">result</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">decodeAuthResponseSecurityBlob</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">security_blob</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">result</span> <span class="o">==</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">RESULT_ACCEPT_COMPLETED</span><span class="p">:</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">True</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) successful!&#39;</span><span class="p">)</span>

                            <span class="c1"># [MS-SMB2]: 3.2.5.3.1</span>
                            <span class="c1"># If the security subsystem indicates that the session was established by an anonymous user,</span>
                            <span class="c1"># Session.SigningRequired MUST be set to FALSE.</span>
                            <span class="c1"># If the SMB2_SESSION_FLAG_IS_GUEST bit is set in the SessionFlags field of the</span>
                            <span class="c1"># SMB2 SESSION_SETUP Response and if Session.SigningRequired is TRUE, this indicates a SESSION_SETUP</span>
                            <span class="c1"># failure and the connection MUST be terminated. If the SMB2_SESSION_FLAG_IS_GUEST bit is set in the SessionFlags</span>
                            <span class="c1"># field of the SMB2 SESSION_SETUP Response and if RequireMessageSigning is FALSE, Session.SigningRequired</span>
                            <span class="c1"># MUST be set to FALSE.</span>
                            <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">isGuestSession</span> <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">isAnonymousSession</span><span class="p">:</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="kc">False</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Signing disabled because session is guest/anonymous&#39;</span><span class="p">)</span>

                            <span class="bp">self</span><span class="o">.</span><span class="n">onAuthOK</span><span class="p">()</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="s1">&#39;SMB2_COM_SESSION_SETUP status is 0 but security blob negResult value is </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">result</span><span class="p">,</span> <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
                    <span class="k">except</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">BadSecurityBlobError</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">ex</span><span class="p">),</span> <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc0000016</span><span class="p">:</span>  <span class="c1"># STATUS_MORE_PROCESSING_REQUIRED</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">session_id</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">session_id</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">result</span><span class="p">,</span> <span class="n">ntlm_token</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">decodeChallengeSecurityBlob</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">security_blob</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">result</span> <span class="o">==</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">RESULT_ACCEPT_INCOMPLETE</span><span class="p">:</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">_handleSessionChallenge</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">ntlm_token</span><span class="p">)</span>
                    <span class="k">except</span> <span class="p">(</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">BadSecurityBlobError</span><span class="p">,</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">UnsupportedSecurityProvider</span> <span class="p">)</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">ex</span><span class="p">),</span> <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
                <span class="k">elif</span> <span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc000006d</span>   <span class="c1"># STATUS_LOGON_FAILURE</span>
                    <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc0000064</span>  <span class="c1"># STATUS_NO_SUCH_USER</span>
                    <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc000006a</span><span class="p">):</span><span class="c1"># STATUS_WRONG_PASSWORD</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) failed. Please check username and password.&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                <span class="k">elif</span> <span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc0000193</span>    <span class="c1"># STATUS_ACCOUNT_EXPIRED</span>
                    <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xC0000071</span><span class="p">):</span> <span class="c1"># STATUS_PASSWORD_EXPIRED</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) failed. Account or password has expired.&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc0000234</span><span class="p">:</span> <span class="c1"># STATUS_ACCOUNT_LOCKED_OUT</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) failed. Account has been locked due to too many invalid logon attempts.&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc0000072</span><span class="p">:</span> <span class="c1"># STATUS_ACCOUNT_DISABLED</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) failed. Account has been disabled.&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                <span class="k">elif</span> <span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc000006f</span>    <span class="c1"># STATUS_INVALID_LOGON_HOURS</span>
                    <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc000015b</span>   <span class="c1"># STATUS_LOGON_TYPE_NOT_GRANTED</span>
                    <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc0000070</span><span class="p">):</span> <span class="c1"># STATUS_INVALID_WORKSTATION</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) failed. Not allowed.&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc000018c</span><span class="p">:</span>  <span class="c1"># STATUS_TRUSTED_DOMAIN_FAILURE</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) failed. Domain not trusted.&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc000018d</span><span class="p">:</span>  <span class="c1"># STATUS_TRUSTED_RELATIONSHIP_FAILURE</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (on SMB2) failed. Workstation not trusted.&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="s1">&#39;Unknown status value (0x</span><span class="si">%08X</span><span class="s1">) in SMB_COM_SESSION_SETUP_ANDX (with extended security)&#39;</span> <span class="o">%</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="p">,</span>
                                        <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">isAsync</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0x00000103</span><span class="p">:</span>  <span class="c1"># STATUS_PENDING</span>
                    <span class="n">req</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">req</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">async_requests</span><span class="p">[</span><span class="n">message</span><span class="o">.</span><span class="n">async_id</span><span class="p">]</span> <span class="o">=</span> <span class="n">req</span>
                <span class="k">else</span><span class="p">:</span> <span class="c1"># All other status including SUCCESS</span>
                    <span class="n">req</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">async_requests</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">async_id</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">req</span><span class="p">:</span>
                        <span class="n">req</span><span class="o">.</span><span class="n">callback</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="o">**</span><span class="n">req</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span>
                        <span class="k">return</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">req</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">req</span><span class="p">:</span>
                    <span class="n">req</span><span class="o">.</span><span class="n">callback</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="o">**</span><span class="n">req</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span>
                    <span class="k">return</span> <span class="kc">True</span>


    <span class="k">def</span> <span class="nf">_updateServerInfo_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">payload</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">capabilities</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">capabilities</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">security_mode</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_transact_size</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">max_transact_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_read_size</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">max_read_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_write_size</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">max_write_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_plaintext_authentication</span> <span class="o">=</span> <span class="kc">False</span>   <span class="c1"># SMB2 never allows plaintext authentication</span>


    <span class="k">def</span> <span class="nf">_handleNegotiateResponse_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="n">ntlm_data</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateNegotiateMessage</span><span class="p">()</span>
        <span class="n">blob</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">generateNegotiateSecurityBlob</span><span class="p">(</span><span class="n">ntlm_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2SessionSetupRequest</span><span class="p">(</span><span class="n">blob</span><span class="p">)))</span>


    <span class="k">def</span> <span class="nf">_handleSessionChallenge_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">ntlm_token</span><span class="p">):</span>
        <span class="n">server_challenge</span><span class="p">,</span> <span class="n">server_flags</span><span class="p">,</span> <span class="n">server_info</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">decodeChallengeMessage</span><span class="p">(</span><span class="n">ntlm_token</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Performing NTLMv2 authentication (on SMB2) with server challenge &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">server_challenge</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_ntlm_v2</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Performing NTLMv2 authentication (on SMB2) with server challenge &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">server_challenge</span><span class="p">))</span>
            <span class="n">nt_challenge_response</span><span class="p">,</span> <span class="n">lm_challenge_response</span><span class="p">,</span> <span class="n">session_key</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateChallengeResponseV2</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">,</span>
                                                                                                         <span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span>
                                                                                                         <span class="n">server_challenge</span><span class="p">,</span>
                                                                                                         <span class="n">server_info</span><span class="p">,</span>
                                                                                                         <span class="bp">self</span><span class="o">.</span><span class="n">domain</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Performing NTLMv1 authentication (on SMB2) with server challenge &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">server_challenge</span><span class="p">))</span>
            <span class="n">nt_challenge_response</span><span class="p">,</span> <span class="n">lm_challenge_response</span><span class="p">,</span> <span class="n">session_key</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateChallengeResponseV1</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">,</span> <span class="n">server_challenge</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>

        <span class="n">ntlm_data</span><span class="p">,</span> <span class="n">session_signing_key</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateAuthenticateMessage</span><span class="p">(</span><span class="n">server_flags</span><span class="p">,</span>
                                                                          <span class="n">nt_challenge_response</span><span class="p">,</span>
                                                                          <span class="n">lm_challenge_response</span><span class="p">,</span>
                                                                          <span class="n">session_key</span><span class="p">,</span>
                                                                          <span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span>
                                                                          <span class="bp">self</span><span class="o">.</span><span class="n">domain</span><span class="p">,</span>
                                                                          <span class="bp">self</span><span class="o">.</span><span class="n">my_name</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">isEnabledFor</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;NT challenge response is &quot;</span><span class="si">%s</span><span class="s1">&quot; (</span><span class="si">%d</span><span class="s1"> bytes)&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">nt_challenge_response</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">nt_challenge_response</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;LM challenge response is &quot;</span><span class="si">%s</span><span class="s1">&quot; (</span><span class="si">%d</span><span class="s1"> bytes)&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">lm_challenge_response</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">lm_challenge_response</span><span class="p">))</span>

        <span class="n">blob</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">generateAuthSecurityBlob</span><span class="p">(</span><span class="n">ntlm_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2SessionSetupRequest</span><span class="p">(</span><span class="n">blob</span><span class="p">)))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">&amp;</span> <span class="n">SMB2_NEGOTIATE_SIGNING_REQUIRED</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Server requires all SMB messages to be signed&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sign_options</span> <span class="o">!=</span> <span class="n">SMB</span><span class="o">.</span><span class="n">SIGN_NEVER</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">&amp;</span> <span class="n">SMB2_NEGOTIATE_SIGNING_ENABLED</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Server supports SMB signing&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sign_options</span> <span class="o">==</span> <span class="n">SMB</span><span class="o">.</span><span class="n">SIGN_WHEN_SUPPORTED</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;SMB signing activated. All SMB messages will be signed.&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">signing_session_key</span> <span class="o">=</span> <span class="n">session_signing_key</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">isEnabledFor</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;SMB signing key is </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">signing_session_key</span><span class="p">))</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">capabilities</span> <span class="o">&amp;</span> <span class="n">CAP_EXTENDED_SECURITY</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">signing_challenge_response</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">signing_challenge_response</span> <span class="o">=</span> <span class="n">blob</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;SMB signing deactivated. SMB messages will NOT be signed.&quot;</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">_listShares_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;IPC$&#39;</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">connectSrvSvc</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="s1">&#39;srvsvc&#39;</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_WRITE_DATA</span> <span class="o">|</span> <span class="n">FILE_APPEND_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_EA</span> <span class="o">|</span> <span class="n">FILE_WRITE_EA</span> <span class="o">|</span> <span class="n">READ_CONTROL</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">FILE_WRITE_ATTRIBUTES</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span> <span class="o">|</span> <span class="n">FILE_SHARE_DELETE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_NON_DIRECTORY_FILE</span> <span class="o">|</span> <span class="n">FILE_OPEN_NO_RECALL</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">))</span>

            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectSrvSvcCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">connectSrvSvcCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">call_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getNextRPCCallID</span><span class="p">()</span>
                <span class="c1"># The data_bytes are binding call to Server Service RPC using DCE v1.1 RPC over SMB. See [MS-SRVS] and [C706]</span>
                <span class="c1"># If you wish to understand the meanings of the byte stream, I would suggest you use a recent version of WireShark to packet capture the stream</span>
                <span class="n">data_bytes</span> <span class="o">=</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;05 00 0b 03 10 00 00 00 74 00 00 00&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="o">+</span> \
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">call_id</span><span class="p">)</span> <span class="o">+</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">b8 10 b8 10 00 00 00 00 02 00 00 00 00 00 01 00</span>
<span class="s2">c8 4f 32 4b 70 16 d3 01 12 78 5a 47 bf 6e e1 88</span>
<span class="s2">03 00 00 00 04 5d 88 8a eb 1c c9 11 9f e8 08 00</span>
<span class="s2">2b 10 48 60 02 00 00 00 01 00 01 00 c8 4f 32 4b</span>
<span class="s2">70 16 d3 01 12 78 5a 47 bf 6e e1 88 03 00 00 00</span>
<span class="s2">2c 1c b7 6c 12 98 40 45 03 00 00 00 00 00 00 00</span>
<span class="s2">01 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2WriteRequest</span><span class="p">(</span><span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">rpcBindCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to locate Server Service RPC endpoint&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">rpcBindCB</span><span class="p">(</span><span class="n">trans_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">trans_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">trans_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2ReadRequest</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">read_len</span> <span class="o">=</span> <span class="mi">1024</span><span class="p">,</span> <span class="n">read_offset</span> <span class="o">=</span> <span class="mi">0</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">rpcReadCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">error</span> <span class="o">=</span> <span class="s1">&#39;Failed to list shares: Unable to read from Server Service RPC endpoint&#39;</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">rpcReadCB</span><span class="p">(</span><span class="n">read_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">read_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">read_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">call_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getNextRPCCallID</span><span class="p">()</span>

                <span class="n">padding</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>
                <span class="n">remote_name</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\\\\</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span>
                <span class="n">server_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">remote_name</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="n">server_bytes_len</span> <span class="o">=</span> <span class="n">server_len</span> <span class="o">*</span> <span class="mi">2</span>
                <span class="k">if</span> <span class="n">server_len</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">padding</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\0\0</span><span class="s1">&#39;</span>
                    <span class="n">server_bytes_len</span> <span class="o">+=</span> <span class="mi">2</span>

                <span class="c1"># The data bytes are the RPC call to NetrShareEnum (Opnum 15) at Server Service RPC.</span>
                <span class="c1"># If you wish to understand the meanings of the byte stream, I would suggest you use a recent version of WireShark to packet capture the stream</span>
                <span class="n">data_bytes</span> <span class="o">=</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;05 00 00 03 10 00 00 00&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="o">+</span> \
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;HHI&#39;</span><span class="p">,</span> <span class="mi">72</span><span class="o">+</span><span class="n">server_bytes_len</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">call_id</span><span class="p">)</span> <span class="o">+</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;4c 00 00 00 00 00 0f 00 00 00 02 00&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="o">+</span> \
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;III&#39;</span><span class="p">,</span> <span class="n">server_len</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">server_len</span><span class="p">)</span> <span class="o">+</span> \
                    <span class="p">(</span><span class="n">remote_name</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">padding</span> <span class="o">+</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">01 00 00 00 01 00 00 00 04 00 02 00 00 00 00 00</span>
<span class="s2">00 00 00 00 ff ff ff ff 08 00 02 00 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2IoctlRequest</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="mh">0x0011C017</span><span class="p">,</span> <span class="n">flags</span> <span class="o">=</span> <span class="mh">0x01</span><span class="p">,</span> <span class="n">max_out_size</span> <span class="o">=</span> <span class="mi">8196</span><span class="p">,</span> <span class="n">in_data</span> <span class="o">=</span> <span class="n">data_bytes</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">listShareResultsCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">error</span> <span class="o">=</span> <span class="s1">&#39;Failed to list shares: Unable to bind to Server Service RPC endpoint&#39;</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">listShareResultsCB</span><span class="p">(</span><span class="n">result_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">result_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">result_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="c1"># The payload.data_bytes will contain the results of the RPC call to NetrShareEnum (Opnum 15) at Server Service RPC.</span>
                <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">result_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">out_data</span>

                <span class="k">if</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&amp;</span> <span class="mh">0x02</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">sendReadRequest</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">data_bytes</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">decodeResults</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">data_bytes</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to retrieve shared device list&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">decodeResults</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">):</span>
            <span class="n">shares_count</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">36</span><span class="p">:</span><span class="mi">40</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>     <span class="c1"># A list of SharedDevice instances</span>
            <span class="n">offset</span> <span class="o">=</span> <span class="mi">36</span> <span class="o">+</span> <span class="mi">12</span>  <span class="c1"># You need to study the byte stream to understand the meaning of these constants</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">shares_count</span><span class="p">):</span>
                <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SharedDevice</span><span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="o">+</span><span class="mi">4</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">8</span><span class="p">])[</span><span class="mi">0</span><span class="p">],</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>
                <span class="n">offset</span> <span class="o">+=</span> <span class="mi">12</span>

            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">shares_count</span><span class="p">):</span>
                <span class="n">max_length</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">length</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;III&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">12</span><span class="p">])</span>
                <span class="n">offset</span> <span class="o">+=</span> <span class="mi">12</span>
                <span class="n">results</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="n">length</span><span class="o">*</span><span class="mi">2</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>

                <span class="k">if</span> <span class="n">length</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span>

                <span class="n">max_length</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">length</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;III&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">12</span><span class="p">])</span>
                <span class="n">offset</span> <span class="o">+=</span> <span class="mi">12</span>
                <span class="n">results</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">comments</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="n">length</span><span class="o">*</span><span class="mi">2</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>

                <span class="k">if</span> <span class="n">length</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span>

            <span class="n">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">callback</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">sendReadRequest</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">):</span>
            <span class="n">read_count</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">4280</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_read_size</span><span class="p">)</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2ReadRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">read_count</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">readCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span>
                                                           <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">data_bytes</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">readCB</span><span class="p">(</span><span class="n">read_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">read_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">read_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span>

                <span class="k">if</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&amp;</span> <span class="mh">0x02</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">sendReadRequest</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_bytes&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">24</span><span class="p">:])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">decodeResults</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_bytes&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">24</span><span class="p">:])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to retrieve shared device list&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">results</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">results</span> <span class="o">=</span> <span class="n">results</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="n">error</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;results&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;results&#39;</span><span class="p">])</span>
            <span class="k">elif</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">],</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">path</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">path</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">connectSrvSvc</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to connect to IPC$&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">path</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">connectSrvSvc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">path</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_listPath_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">search</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span> <span class="o">|</span> <span class="n">FILE_SHARE_DELETE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_DIRECTORY_FILE</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendQuery</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xC0000034</span><span class="p">:</span> <span class="c1"># [MS-ERREF]: STATUS_OBJECT_NAME_INVALID</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Path not found&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open directory&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendQuery</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_buf</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2QueryDirectoryRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span>
                                                      <span class="n">info_class</span> <span class="o">=</span> <span class="mh">0x25</span><span class="p">,</span>   <span class="c1"># FileIdBothDirectoryInformation</span>
                                                      <span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                      <span class="n">output_buf_len</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_transact_size</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">queryCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_buf</span> <span class="o">=</span> <span class="n">data_buf</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">queryCB</span><span class="p">(</span><span class="n">query_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">query_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">query_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">data_buf</span> <span class="o">=</span> <span class="n">decodeQueryStruct</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_buf&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">query_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
                <span class="n">sendQuery</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">data_buf</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">query_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xC000000F</span><span class="p">:</span> <span class="c1"># [MS-ERREF]: STATUS_NO_SUCH_FILE</span>
                <span class="c1"># If there are no matching files, we just treat as success instead of failing</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">results</span> <span class="o">=</span> <span class="n">results</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">query_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0x80000006</span><span class="p">:</span>  <span class="c1"># STATUS_NO_MORE_FILES</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">results</span> <span class="o">=</span> <span class="n">results</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">error</span> <span class="o">=</span> <span class="n">query_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">decodeQueryStruct</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">):</span>
            <span class="c1"># FileIdBothDirectoryInformation structure. See [MS-SMB]: 2.2.8.1.3 and [MS-FSCC]: 2.4.17</span>
            <span class="n">info_format</span> <span class="o">=</span> <span class="s1">&#39;&lt;IIQQQQQQIIIBB24sHQ&#39;</span>
            <span class="n">info_size</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">calcsize</span><span class="p">(</span><span class="n">info_format</span><span class="p">)</span>

            <span class="n">data_length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">)</span>
            <span class="n">offset</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="n">offset</span> <span class="o">&lt;</span> <span class="n">data_length</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">offset</span> <span class="o">+</span> <span class="n">info_size</span> <span class="o">&gt;</span> <span class="n">data_length</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:]</span>

                <span class="n">next_offset</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> \
                <span class="n">create_time</span><span class="p">,</span> <span class="n">last_access_time</span><span class="p">,</span> <span class="n">last_write_time</span><span class="p">,</span> <span class="n">last_attr_change_time</span><span class="p">,</span> \
                <span class="n">file_size</span><span class="p">,</span> <span class="n">alloc_size</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="n">filename_length</span><span class="p">,</span> <span class="n">ea_size</span><span class="p">,</span> \
                <span class="n">short_name_length</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">short_name</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">file_id</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="n">info_format</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="n">info_size</span><span class="p">])</span>

                <span class="n">offset2</span> <span class="o">=</span> <span class="n">offset</span> <span class="o">+</span> <span class="n">info_size</span>
                <span class="k">if</span> <span class="n">offset2</span> <span class="o">+</span> <span class="n">filename_length</span> <span class="o">&gt;</span> <span class="n">data_length</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:]</span>

                <span class="n">filename</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">[</span><span class="n">offset2</span><span class="p">:</span><span class="n">offset2</span><span class="o">+</span><span class="n">filename_length</span><span class="p">])</span>
                <span class="n">short_name</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">short_name</span><span class="p">[:</span><span class="n">short_name_length</span><span class="p">])</span>

                <span class="n">accept_result</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">file_attributes</span> <span class="o">&amp;</span> <span class="mh">0xff</span><span class="p">)</span> <span class="ow">in</span> <span class="p">(</span> <span class="mh">0x00</span><span class="p">,</span> <span class="n">ATTR_NORMAL</span> <span class="p">):</span> <span class="c1"># Only the first 8-bits are compared. We ignore other bits like temp, compressed, encryption, sparse, indexed, etc</span>
                    <span class="n">accept_result</span> <span class="o">=</span> <span class="p">(</span><span class="n">search</span> <span class="o">==</span> <span class="n">SMB_FILE_ATTRIBUTE_NORMAL</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">search</span> <span class="o">&amp;</span> <span class="n">SMB_FILE_ATTRIBUTE_INCL_NORMAL</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">accept_result</span> <span class="o">=</span> <span class="p">(</span><span class="n">file_attributes</span> <span class="o">&amp;</span> <span class="n">search</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>
                <span class="k">if</span> <span class="n">accept_result</span><span class="p">:</span>
                    <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SharedFile</span><span class="p">(</span><span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">create_time</span><span class="p">),</span> <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_access_time</span><span class="p">),</span>
                                              <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_write_time</span><span class="p">),</span> <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_attr_change_time</span><span class="p">),</span>
                                              <span class="n">file_size</span><span class="p">,</span> <span class="n">alloc_size</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="n">short_name</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">file_id</span><span class="p">))</span>

                <span class="k">if</span> <span class="n">next_offset</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="n">next_offset</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">break</span>
            <span class="k">return</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">results</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">results</span> <span class="o">=</span> <span class="n">results</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="n">error</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;results&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;results&#39;</span><span class="p">])</span>
            <span class="k">elif</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mh">0xC000000F</span><span class="p">:</span>  <span class="c1"># [MS-ERREF]: STATUS_NO_SUCH_FILE</span>
                    <span class="c1"># Remote server returns STATUS_NO_SUCH_FILE error so we assume that the search returns no matching files</span>
                    <span class="n">callback</span><span class="p">([</span> <span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Query failed with errorcode 0x</span><span class="si">%08x</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_getAttributes_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_EA</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span> <span class="o">|</span> <span class="n">FILE_SHARE_DELETE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">p</span> <span class="o">=</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span>
                <span class="n">filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extractLastPathComponent</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
                <span class="n">info</span> <span class="o">=</span> <span class="n">SharedFile</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">create_time</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">lastaccess_time</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">lastwrite_time</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">change_time</span><span class="p">,</span>
                                  <span class="n">p</span><span class="o">.</span><span class="n">file_size</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">allocation_size</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">file_attributes</span><span class="p">,</span>
                                  <span class="n">filename</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">p</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="n">info</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get attributes for </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open remote file object&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="n">info</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="n">error</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;info&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;info&#39;</span><span class="p">])</span>
            <span class="k">elif</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get attributes for </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Query failed with errorcode 0x</span><span class="si">%08x</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get attributes for </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_getSecurity_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_EA</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">READ_CONTROL</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span> <span class="o">|</span> <span class="n">FILE_SHARE_DELETE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2QueryInfoRequest</span><span class="p">(</span><span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span>
                                                     <span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                     <span class="n">additional_info</span> <span class="o">=</span> <span class="n">OWNER_SECURITY_INFORMATION</span> <span class="o">|</span> <span class="n">GROUP_SECURITY_INFORMATION</span> <span class="o">|</span> <span class="n">DACL_SECURITY_INFORMATION</span><span class="p">,</span>
                                                     <span class="n">info_type</span> <span class="o">=</span> <span class="n">SMB2_INFO_SECURITY</span><span class="p">,</span>
                                                     <span class="n">file_info_class</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="c1"># [MS-SMB2] 2.2.37, 3.2.4.12</span>
                                                     <span class="n">input_buf</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                                                     <span class="n">output_buf_len</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_transact_size</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">queryCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get the security descriptor of </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file or directory&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">queryCB</span><span class="p">(</span><span class="n">query_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">query_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">query_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">security</span> <span class="o">=</span> <span class="n">SecurityDescriptor</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">query_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">result</span> <span class="o">=</span> <span class="n">security</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">error</span> <span class="o">=</span> <span class="n">query_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">result</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="n">error</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">])</span>
            <span class="k">elif</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get the security descriptor of </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Query failed with errorcode 0x</span><span class="si">%08x</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get the security descriptor of </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_retrieveFile_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFileFromOffset</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_retrieveFileFromOffset_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">starting_offset</span><span class="p">,</span> <span class="n">max_length</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_EA</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">READ_CONTROL</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_SEQUENTIAL_ONLY</span> <span class="o">|</span> <span class="n">FILE_NON_DIRECTORY_FILE</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2QueryInfoRequest</span><span class="p">(</span><span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span>
                                                     <span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                     <span class="n">additional_info</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                     <span class="n">info_type</span> <span class="o">=</span> <span class="n">SMB2_INFO_FILE</span><span class="p">,</span>
                                                     <span class="n">file_info_class</span> <span class="o">=</span> <span class="mh">0x05</span><span class="p">,</span>  <span class="c1"># FileStandardInformation [MS-FSCC] 2.4</span>
                                                     <span class="n">input_buf</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
                                                     <span class="n">output_buf_len</span> <span class="o">=</span> <span class="mi">4096</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">infoCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span>
                                                               <span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span>
                                                               <span class="n">fid</span> <span class="o">=</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span>
                                                               <span class="n">file_attributes</span> <span class="o">=</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">file_attributes</span><span class="p">)</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to retrieve </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">infoCB</span><span class="p">(</span><span class="n">info_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">info_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">info_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">file_len</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;Q&#39;</span><span class="p">,</span> <span class="n">info_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">16</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">max_length</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">starting_offset</span> <span class="o">&gt;</span> <span class="n">file_len</span><span class="p">:</span>
                    <span class="n">closeFid</span><span class="p">(</span><span class="n">info_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                    <span class="n">callback</span><span class="p">((</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;file_attributes&#39;</span><span class="p">],</span> <span class="mi">0</span> <span class="p">))</span>  <span class="c1"># Note that this is a tuple of 3-elements</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">remaining_len</span> <span class="o">=</span> <span class="n">max_length</span>
                    <span class="k">if</span> <span class="n">remaining_len</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">remaining_len</span> <span class="o">=</span> <span class="n">file_len</span>
                    <span class="k">if</span> <span class="n">starting_offset</span> <span class="o">+</span> <span class="n">remaining_len</span> <span class="o">&gt;</span> <span class="n">file_len</span><span class="p">:</span>
                        <span class="n">remaining_len</span> <span class="o">=</span> <span class="n">file_len</span> <span class="o">-</span> <span class="n">starting_offset</span>
                    <span class="n">sendRead</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">starting_offset</span><span class="p">,</span> <span class="n">remaining_len</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;file_attributes&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to retrieve </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to retrieve information on file&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendRead</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">remaining_len</span><span class="p">,</span> <span class="n">read_len</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">):</span>
            <span class="n">read_count</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_read_size</span><span class="p">,</span> <span class="n">remaining_len</span><span class="p">)</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2ReadRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">read_count</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">readCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span>
                                                           <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="p">,</span>
                                                           <span class="n">remaining_len</span> <span class="o">=</span> <span class="n">remaining_len</span><span class="p">,</span>
                                                           <span class="n">read_len</span> <span class="o">=</span> <span class="n">read_len</span><span class="p">,</span>
                                                           <span class="n">file_attributes</span> <span class="o">=</span> <span class="n">file_attributes</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">readCB</span><span class="p">(</span><span class="n">read_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="c1"># To avoid crazy memory usage when retrieving large files, we do not save every read_message in messages_history.</span>
            <span class="k">if</span> <span class="n">read_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">data_len</span> <span class="o">=</span> <span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_length</span>
                <span class="n">file_obj</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>

                <span class="n">remaining_len</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;remaining_len&#39;</span><span class="p">]</span> <span class="o">-</span> <span class="n">data_len</span>

                <span class="k">if</span> <span class="n">remaining_len</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">sendRead</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data_len</span><span class="p">,</span> <span class="n">remaining_len</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;read_len&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data_len</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;file_attributes&#39;</span><span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">ret</span> <span class="o">=</span> <span class="p">(</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;file_attributes&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;read_len&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data_len</span> <span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">read_message</span><span class="p">)</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">error</span> <span class="o">=</span> <span class="n">read_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">ret</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">ret</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="n">error</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;ret&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;ret&#39;</span><span class="p">])</span>
            <span class="k">elif</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to retrieve </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Read failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to retrieve </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_storeFile_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_storeFileFromOffset_SMB2</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_storeFileFromOffset_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">starting_offset</span><span class="p">,</span> <span class="n">truncate</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 20 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 08 00 00 00 41 6c 53 69 00 00 00 00</span>
<span class="s2">85 62 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="n">ATTR_ARCHIVE</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_WRITE_DATA</span> <span class="o">|</span> <span class="n">FILE_APPEND_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">FILE_WRITE_ATTRIBUTES</span> <span class="o">|</span> <span class="n">FILE_READ_EA</span> <span class="o">|</span> <span class="n">FILE_WRITE_EA</span> <span class="o">|</span> <span class="n">READ_CONTROL</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_SEQUENTIAL_ONLY</span> <span class="o">|</span> <span class="n">FILE_NON_DIRECTORY_FILE</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OVERWRITE_IF</span> <span class="k">if</span> <span class="n">truncate</span> <span class="k">else</span> <span class="n">FILE_OPEN_IF</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">create_message</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">]</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendWrite</span><span class="p">(</span><span class="n">create_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span> <span class="n">starting_offset</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to store </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendWrite</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span><span class="p">):</span>
            <span class="n">write_count</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_write_size</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">file_obj</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">write_count</span><span class="p">)</span>
            <span class="n">data_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">data_len</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2WriteRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">offset</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">writeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="o">+</span><span class="n">data_len</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">writeCB</span><span class="p">(</span><span class="n">write_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="c1"># To avoid crazy memory usage when saving large files, we do not save every write_message in messages_history.</span>
            <span class="k">if</span> <span class="n">write_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendWrite</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">write_message</span><span class="p">)</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to store </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Write failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="n">error</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">((</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">]</span> <span class="p">))</span>  <span class="c1"># Note that this is a tuple of 2-elements</span>
            <span class="k">elif</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to store </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Write failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to store </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>


    <span class="k">def</span> <span class="nf">_deleteFiles_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path_file_pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">pattern</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path_file_pattern</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">path_components</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;*&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">or</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;?&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path_components</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">pattern</span> <span class="o">=</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span><span class="p">,</span> <span class="n">files_queue</span> <span class="o">=</span> <span class="p">[</span> <span class="p">],</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">if</span> <span class="n">pattern</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">path_components</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">path_components</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">files_queue</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path_components</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">files_queue</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">path</span> <span class="p">))</span>

        <span class="k">def</span> <span class="nf">deleteCB</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">files_queue</span><span class="p">:</span>
                <span class="n">p</span><span class="p">,</span> <span class="n">filename</span> <span class="o">=</span> <span class="n">files_queue</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">filename</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">p</span><span class="p">:</span>
                        <span class="n">filename</span> <span class="o">=</span> <span class="n">p</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">filename</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB2__del</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">],</span> <span class="n">filename</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_deleteDirectory_SMB2</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path_file_pattern</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">listCB</span><span class="p">(</span><span class="n">files_list</span><span class="p">):</span>
            <span class="n">files_queue</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">files_list</span><span class="p">)</span>
            <span class="n">deleteCB</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="k">if</span> <span class="n">files_queue</span><span class="p">:</span>
                        <span class="n">deleteCB</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB2__list</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">files_queue</span><span class="p">:</span>
                <span class="n">deleteCB</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB2__list</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_deleteFiles_SMB2__list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="n">folder_queue</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">files_list</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">current_path</span> <span class="o">=</span> <span class="p">[</span> <span class="n">path</span> <span class="p">]</span>
        <span class="n">search</span> <span class="o">=</span> <span class="n">SMB_FILE_ATTRIBUTE_READONLY</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_HIDDEN</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_SYSTEM</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_DIRECTORY</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_ARCHIVE</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_INCL_NORMAL</span>

        <span class="k">def</span> <span class="nf">listCB</span><span class="p">(</span><span class="n">results</span><span class="p">):</span>
            <span class="n">files</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">filename</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span> <span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="s1">&#39;..&#39;</span> <span class="p">],</span> <span class="n">results</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">isDirectory</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">delete_matching_folders</span><span class="p">:</span>
                        <span class="n">folder_queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="o">+</span><span class="n">f</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">files</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">f</span><span class="o">.</span><span class="n">filename</span> <span class="p">))</span>
            <span class="k">if</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">!=</span><span class="n">path</span> <span class="ow">and</span> <span class="n">delete_matching_folders</span><span class="p">:</span>
                <span class="n">files</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="kc">None</span> <span class="p">))</span>

            <span class="k">if</span> <span class="n">files</span><span class="p">:</span>
                <span class="n">files_list</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">files</span>

            <span class="k">if</span> <span class="n">folder_queue</span><span class="p">:</span>
                <span class="n">p</span> <span class="o">=</span> <span class="n">folder_queue</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
                <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">p</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_listPath_SMB2</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">search</span> <span class="o">=</span> <span class="n">search</span><span class="p">,</span> <span class="n">pattern</span> <span class="o">=</span> <span class="s1">&#39;*&#39;</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">files_list</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_listPath_SMB2</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">search</span> <span class="o">=</span> <span class="n">search</span><span class="p">,</span> <span class="n">pattern</span> <span class="o">=</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_deleteFiles_SMB2__del</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">tid</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">    28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">    44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">    00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">    00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">    00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">    51 46 69 64 00 00 00 00</span>
<span class="s2">    &quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">DELETE</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span> <span class="o">|</span> <span class="n">FILE_SHARE_DELETE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_NON_DIRECTORY_FILE</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">open_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">open_message</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">]</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">open_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendDelete</span><span class="p">(</span><span class="n">open_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xc0000034</span><span class="p">:</span>  <span class="c1"># STATUS_OBJECT_NAME_NOT_FOUND</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mh">0xC00000BA</span><span class="p">:</span>  <span class="c1"># [MS-ERREF]: STATUS_FILE_IS_A_DIRECTORY</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Cannot delete a folder. Please use deleteDirectory() method or append &quot;/*&quot; to your path if you wish to delete all files in the folder.&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendDelete</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2SetInfoRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span>
                                               <span class="n">additional_info</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                               <span class="n">info_type</span> <span class="o">=</span> <span class="n">SMB2_INFO_FILE</span><span class="p">,</span>
                                               <span class="n">file_info_class</span> <span class="o">=</span> <span class="mh">0x0d</span><span class="p">,</span>  <span class="c1"># SMB2_FILE_DISPOSITION_INFO</span>
                                               <span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x01</span><span class="s1">&#39;</span><span class="p">))</span>
            <span class="c1"># [MS-SMB2]: 2.2.39, [MS-FSCC]: 2.4.11</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">deleteCB</span><span class="p">(</span><span class="n">delete_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">delete_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;status&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Delete failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="n">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_resetFileAttributes_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path_file_pattern</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">file_attributes</span> <span class="o">=</span> <span class="n">ATTR_NORMAL</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path_file_pattern</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_WRITE_ATTRIBUTES</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">open_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">open_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendReset</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to reset attributes of </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendReset</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2SetInfoRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span>
                                               <span class="n">additional_info</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                               <span class="n">info_type</span> <span class="o">=</span> <span class="n">SMB2_INFO_FILE</span><span class="p">,</span>
                                               <span class="n">file_info_class</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span>  <span class="c1"># FileBasicInformation</span>
                                               <span class="n">data</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;qqqqii&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="mi">0</span><span class="p">)))</span>
            <span class="c1"># [MS-SMB2]: 2.2.39, [MS-FSCC]: 2.4, [MS-FSCC]: 2.4.7, [MS-FSCC]: 2.6</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">resetCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">resetCB</span><span class="p">(</span><span class="n">reset_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reset_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">reset_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="n">reset_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;status&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path_file_pattern</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to reset attributes of </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Reset failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to reset attributes of </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_createDirectory_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_WRITE_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_EA</span> <span class="o">|</span> <span class="n">FILE_WRITE_EA</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">FILE_WRITE_ATTRIBUTES</span> <span class="o">|</span> <span class="n">READ_CONTROL</span> <span class="o">|</span> <span class="n">DELETE</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_DIRECTORY_FILE</span> <span class="o">|</span> <span class="n">FILE_SYNCHRONOUS_IO_NONALERT</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_CREATE</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to create directory </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Create failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">callback</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to create directory </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_deleteDirectory_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">DELETE</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span> <span class="o">|</span> <span class="n">FILE_SHARE_DELETE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_DIRECTORY_FILE</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">open_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">open_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendDelete</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open directory&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendDelete</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2SetInfoRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span>
                                               <span class="n">additional_info</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                               <span class="n">info_type</span> <span class="o">=</span> <span class="n">SMB2_INFO_FILE</span><span class="p">,</span>
                                               <span class="n">file_info_class</span> <span class="o">=</span> <span class="mh">0x0d</span><span class="p">,</span>  <span class="c1"># SMB2_FILE_DISPOSITION_INFO</span>
                                               <span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x01</span><span class="s1">&#39;</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">deleteCB</span><span class="p">(</span><span class="n">delete_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">delete_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;status&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Delete failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_rename_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">new_path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="n">new_path</span> <span class="o">=</span> <span class="n">new_path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">new_path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">new_path</span> <span class="o">=</span> <span class="n">new_path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">new_path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">new_path</span> <span class="o">=</span> <span class="n">new_path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

        <span class="n">old_path</span> <span class="o">=</span> <span class="n">old_path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">old_path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">old_path</span> <span class="o">=</span> <span class="n">old_path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">old_path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">old_path</span> <span class="o">=</span> <span class="n">old_path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 18 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">00 00 00 00 10 00 04 00 00 00 18 00 00 00 00 00</span>
<span class="s2">51 46 69 64 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">old_path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">DELETE</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span> <span class="o">|</span> <span class="n">FILE_SHARE_DELETE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_SYNCHRONOUS_IO_NONALERT</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendRename</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to rename </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file/directory&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendRename</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span><span class="o">*</span><span class="mi">16</span> <span class="o">+</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_path</span><span class="p">)</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">new_path</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2SetInfoRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span>
                                               <span class="n">additional_info</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                               <span class="n">info_type</span> <span class="o">=</span> <span class="n">SMB2_INFO_FILE</span><span class="p">,</span>
                                               <span class="n">file_info_class</span> <span class="o">=</span> <span class="mh">0x0a</span><span class="p">,</span>  <span class="c1"># SMB2_FILE_RENAME_INFO</span>
                                               <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">renameCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">renameCB</span><span class="p">(</span><span class="n">rename_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rename_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">rename_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="n">rename_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;status&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">((</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">new_path</span> <span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to rename </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Rename failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to rename </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_listSnapshots_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">28 00 00 00 10 00 04 00 00 00 18 00 10 00 00 00</span>
<span class="s2">44 48 6e 51 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span class="s2">00 00 00 00 00 00 00 00 00 00 00 00 10 00 04 00</span>
<span class="s2">00 00 18 00 00 00 00 00 4d 78 41 63 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CreateRequest</span><span class="p">(</span><span class="n">path</span><span class="p">,</span>
                                              <span class="n">file_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">access_mask</span> <span class="o">=</span> <span class="n">FILE_READ_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">SYNCHRONIZE</span><span class="p">,</span>
                                              <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span><span class="p">,</span>
                                              <span class="n">oplock</span> <span class="o">=</span> <span class="n">SMB2_OPLOCK_LEVEL_NONE</span><span class="p">,</span>
                                              <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                              <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_SYNCHRONOUS_IO_NONALERT</span><span class="p">,</span>
                                              <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                              <span class="n">create_context_data</span> <span class="o">=</span> <span class="n">create_context_data</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">sendEnumSnapshots</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list snapshots </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file/directory&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendEnumSnapshots</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2IoctlRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span>
                                             <span class="n">ctlcode</span> <span class="o">=</span> <span class="mh">0x00144064</span><span class="p">,</span>  <span class="c1"># FSCTL_SRV_ENUMERATE_SNAPSHOTS</span>
                                             <span class="n">flags</span> <span class="o">=</span> <span class="mh">0x0001</span><span class="p">,</span>
                                             <span class="n">in_data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">enumSnapshotsCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">enumSnapshotsCB</span><span class="p">(</span><span class="n">enum_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">enum_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">enum_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
                <span class="n">snapshots_count</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">enum_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">out_data</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">8</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">snapshots_count</span><span class="p">):</span>
                    <span class="n">s</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">enum_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">out_data</span><span class="p">[</span><span class="mi">12</span><span class="o">+</span><span class="n">i</span><span class="o">*</span><span class="mi">50</span><span class="p">:</span><span class="mi">12</span><span class="o">+</span><span class="mi">48</span><span class="o">+</span><span class="n">i</span><span class="o">*</span><span class="mi">50</span><span class="p">])</span>
                    <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="o">*</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="p">(</span> <span class="n">s</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">9</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">10</span><span class="p">:</span><span class="mi">12</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">13</span><span class="p">:</span><span class="mi">15</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">16</span><span class="p">:</span><span class="mi">18</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">19</span><span class="p">:</span><span class="mi">21</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">22</span><span class="p">:</span><span class="mi">24</span><span class="p">]</span> <span class="p">)))))</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">results</span> <span class="o">=</span> <span class="n">results</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">status</span> <span class="o">=</span> <span class="n">enum_message</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">results</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2CloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">closeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">status</span> <span class="o">=</span> <span class="n">status</span><span class="p">,</span> <span class="n">results</span> <span class="o">=</span> <span class="n">results</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">closeCB</span><span class="p">(</span><span class="n">close_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;results&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;results&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list snapshots </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: List failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list snapshots </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2TreeConnectRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_echo_SMB2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">echoCB</span><span class="p">(</span><span class="n">echo_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">echo_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">echo_message</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Echo failed&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="n">m</span> <span class="o">=</span> <span class="n">SMB2Message</span><span class="p">(</span><span class="n">SMB2EchoRequest</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">echoCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
        <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>


    <span class="c1">#</span>
    <span class="c1"># SMB1 Methods Family</span>
    <span class="c1">#</span>

    <span class="k">def</span> <span class="nf">_sendSMBMessage_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">smb_message</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">mid</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">mid</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getNextMID_SMB1</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">uid</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uid</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">flags2</span> <span class="o">|=</span> <span class="n">SMB_FLAGS2_SMB_SECURITY_SIGNATURE</span>

            <span class="c1"># Increment the next_signing_id as described in [MS-CIFS] 3.2.4.1.3</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">security</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">next_signing_id</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">next_signing_id</span> <span class="o">+=</span> <span class="mi">2</span>  <span class="c1"># All our defined messages currently have responses, so always increment by 2</span>
            <span class="n">raw_data</span> <span class="o">=</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>

            <span class="n">md</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">MD5</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">signing_session_key</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">signing_challenge_response</span><span class="p">:</span>
                <span class="n">md</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">signing_challenge_response</span><span class="p">)</span>
            <span class="n">md</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">raw_data</span><span class="p">)</span>
            <span class="n">signature</span> <span class="o">=</span> <span class="n">md</span><span class="o">.</span><span class="n">digest</span><span class="p">()[:</span><span class="mi">8</span><span class="p">]</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;MID is </span><span class="si">%d</span><span class="s1">. Signing ID is </span><span class="si">%d</span><span class="s1">. Signature is </span><span class="si">%s</span><span class="s1">. Total raw message is </span><span class="si">%d</span><span class="s1"> bytes&#39;</span><span class="p">,</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">security</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">signature</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">raw_data</span><span class="p">))</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">raw_data</span> <span class="o">=</span> <span class="n">raw_data</span><span class="p">[:</span><span class="mi">14</span><span class="p">]</span> <span class="o">+</span> <span class="n">signature</span> <span class="o">+</span> <span class="n">raw_data</span><span class="p">[</span><span class="mi">22</span><span class="p">:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">smb_message</span><span class="o">.</span><span class="n">raw_data</span> <span class="o">=</span> <span class="n">smb_message</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sendNMBMessage</span><span class="p">(</span><span class="n">smb_message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_getNextMID_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mid</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mid</span> <span class="o">&gt;=</span> <span class="mh">0xFFFF</span><span class="p">:</span> <span class="c1"># MID cannot be 0xFFFF. [MS-CIFS]: 2.2.1.6.2</span>
            <span class="c1"># We don&#39;t use MID of 0 as MID can be reused for SMB_COM_TRANSACTION2_SECONDARY messages</span>
            <span class="c1"># where if mid=0, _sendSMBMessage will re-assign new MID values again</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">mid</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mid</span>

    <span class="k">def</span> <span class="nf">_updateState_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">isReply</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">command</span> <span class="o">==</span> <span class="n">SMB_COM_NEGOTIATE</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">has_negotiated</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;SMB dialect negotiation successful (ExtendedSecurity:</span><span class="si">%s</span><span class="s1">)&#39;</span><span class="p">,</span> <span class="n">message</span><span class="o">.</span><span class="n">hasExtendedSecurity</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_updateServerInfo</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_handleNegotiateResponse</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="s1">&#39;Unknown status value (0x</span><span class="si">%08X</span><span class="s1">) in SMB_COM_NEGOTIATE&#39;</span> <span class="o">%</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span><span class="p">,</span>
                                        <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">command</span> <span class="o">==</span> <span class="n">SMB_COM_SESSION_SETUP_ANDX</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">hasExtendedSecurity</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">result</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">decodeAuthResponseSecurityBlob</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">security_blob</span><span class="p">)</span>
                            <span class="k">if</span> <span class="n">result</span> <span class="o">==</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">RESULT_ACCEPT_COMPLETED</span><span class="p">:</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;SMB uid is now </span><span class="si">%d</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span><span class="p">)</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">True</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) successful!&#39;</span><span class="p">)</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">onAuthOK</span><span class="p">()</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="s1">&#39;SMB_COM_SESSION_SETUP_ANDX status is 0 but security blob negResult value is </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">result</span><span class="p">,</span> <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
                        <span class="k">except</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">BadSecurityBlobError</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
                            <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">ex</span><span class="p">),</span> <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc0000016</span><span class="p">:</span>  <span class="c1"># STATUS_MORE_PROCESSING_REQUIRED</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">result</span><span class="p">,</span> <span class="n">ntlm_token</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">decodeChallengeSecurityBlob</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">security_blob</span><span class="p">)</span>
                            <span class="k">if</span> <span class="n">result</span> <span class="o">==</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">RESULT_ACCEPT_INCOMPLETE</span><span class="p">:</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">_handleSessionChallenge</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">ntlm_token</span><span class="p">)</span>
                        <span class="k">except</span> <span class="p">(</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">BadSecurityBlobError</span><span class="p">,</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">UnsupportedSecurityProvider</span> <span class="p">)</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
                            <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">ex</span><span class="p">),</span> <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc000006d</span>    <span class="c1"># STATUS_LOGON_FAILURE</span>
                        <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc0000064</span>   <span class="c1"># STATUS_NO_SUCH_USER</span>
                        <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc000006a</span><span class="p">):</span> <span class="c1"># STATUS_WRONG_PASSWORD</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) failed. Please check username and password.&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                    <span class="k">elif</span> <span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc0000193</span>    <span class="c1"># STATUS_ACCOUNT_EXPIRED</span>
                        <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xC0000071</span><span class="p">):</span> <span class="c1"># STATUS_PASSWORD_EXPIRED</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) failed. Account or password has expired.&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                    <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc0000234</span><span class="p">:</span> <span class="c1"># STATUS_ACCOUNT_LOCKED_OUT</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) failed. Account has been locked due to too many invalid logon attempts.&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                    <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc0000072</span><span class="p">:</span> <span class="c1"># STATUS_ACCOUNT_DISABLED</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) failed. Account has been disabled.&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                    <span class="k">elif</span> <span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc000006f</span>    <span class="c1"># STATUS_INVALID_LOGON_HOURS</span>
                        <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc000015b</span>   <span class="c1"># STATUS_LOGON_TYPE_NOT_GRANTED</span>
                        <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc0000070</span><span class="p">):</span> <span class="c1"># STATUS_INVALID_WORKSTATION</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) failed. Not allowed.&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                    <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc000018c</span><span class="p">:</span>  <span class="c1"># STATUS_TRUSTED_DOMAIN_FAILURE</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) failed. Domain not trusted.&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                    <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xc000018d</span><span class="p">:</span>  <span class="c1"># STATUS_TRUSTED_RELATIONSHIP_FAILURE</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (with extended security) failed. Workstation not trusted.&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">ProtocolError</span><span class="p">(</span><span class="s1">&#39;Unknown status value (0x</span><span class="si">%08X</span><span class="s1">) in SMB_COM_SESSION_SETUP_ANDX (with extended security)&#39;</span> <span class="o">%</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span><span class="p">,</span>
                                            <span class="n">message</span><span class="o">.</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;SMB uid is now </span><span class="si">%d</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">True</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (without extended security) successful!&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthOK</span><span class="p">()</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Authentication (without extended security) failed. Please check username and password&#39;</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">onAuthFailed</span><span class="p">()</span>
            <span class="k">elif</span> <span class="n">message</span><span class="o">.</span><span class="n">command</span> <span class="o">==</span> <span class="n">SMB_COM_TREE_CONNECT_ANDX</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">req</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">message</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span>
                <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">req</span><span class="o">.</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;path&#39;</span><span class="p">]]</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">tid</span>

            <span class="n">req</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">req</span><span class="p">:</span>
                <span class="n">req</span><span class="o">.</span><span class="n">callback</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="o">**</span><span class="n">req</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span>
                <span class="k">return</span> <span class="kc">True</span>


    <span class="k">def</span> <span class="nf">_updateServerInfo_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">payload</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">capabilities</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">capabilities</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">security_mode</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_raw_size</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">max_raw_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_buffer_size</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">max_buffer_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_mpx_count</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">max_mpx_count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_plaintext_authentication</span> <span class="o">=</span> <span class="ow">not</span> <span class="nb">bool</span><span class="p">(</span><span class="n">payload</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">&amp;</span> <span class="n">NEGOTIATE_ENCRYPT_PASSWORDS</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_plaintext_authentication</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;Remote server only supports plaintext authentication. Your password can be stolen easily over the network.&#39;</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">_handleSessionChallenge_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">ntlm_token</span><span class="p">):</span>
        <span class="k">assert</span> <span class="n">message</span><span class="o">.</span><span class="n">hasExtendedSecurity</span>

        <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">uid</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span>

        <span class="n">server_challenge</span><span class="p">,</span> <span class="n">server_flags</span><span class="p">,</span> <span class="n">server_info</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">decodeChallengeMessage</span><span class="p">(</span><span class="n">ntlm_token</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_ntlm_v2</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Performing NTLMv2 authentication (with extended security) with server challenge &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">server_challenge</span><span class="p">))</span>
            <span class="n">nt_challenge_response</span><span class="p">,</span> <span class="n">lm_challenge_response</span><span class="p">,</span> <span class="n">session_key</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateChallengeResponseV2</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">,</span>
                                                                                                         <span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span>
                                                                                                         <span class="n">server_challenge</span><span class="p">,</span>
                                                                                                         <span class="n">server_info</span><span class="p">,</span>
                                                                                                         <span class="bp">self</span><span class="o">.</span><span class="n">domain</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Performing NTLMv1 authentication (with extended security) with server challenge &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">server_challenge</span><span class="p">))</span>
            <span class="n">nt_challenge_response</span><span class="p">,</span> <span class="n">lm_challenge_response</span><span class="p">,</span> <span class="n">session_key</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateChallengeResponseV1</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">,</span> <span class="n">server_challenge</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>

        <span class="n">ntlm_data</span><span class="p">,</span> <span class="n">signing_session_key</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateAuthenticateMessage</span><span class="p">(</span><span class="n">server_flags</span><span class="p">,</span>
                                                                          <span class="n">nt_challenge_response</span><span class="p">,</span>
                                                                          <span class="n">lm_challenge_response</span><span class="p">,</span>
                                                                          <span class="n">session_key</span><span class="p">,</span>
                                                                          <span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span>
                                                                          <span class="bp">self</span><span class="o">.</span><span class="n">domain</span><span class="p">,</span>
                                                                          <span class="bp">self</span><span class="o">.</span><span class="n">my_name</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">isEnabledFor</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;NT challenge response is &quot;</span><span class="si">%s</span><span class="s1">&quot; (</span><span class="si">%d</span><span class="s1"> bytes)&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">nt_challenge_response</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">nt_challenge_response</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;LM challenge response is &quot;</span><span class="si">%s</span><span class="s1">&quot; (</span><span class="si">%d</span><span class="s1"> bytes)&#39;</span><span class="p">,</span> <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">lm_challenge_response</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">lm_challenge_response</span><span class="p">))</span>

        <span class="n">blob</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">generateAuthSecurityBlob</span><span class="p">(</span><span class="n">ntlm_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComSessionSetupAndxRequest__WithSecurityExtension</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">blob</span><span class="p">)))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">&amp;</span> <span class="n">NEGOTIATE_SECURITY_SIGNATURES_REQUIRE</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Server requires all SMB messages to be signed&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sign_options</span> <span class="o">!=</span> <span class="n">SMB</span><span class="o">.</span><span class="n">SIGN_NEVER</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">security_mode</span> <span class="o">&amp;</span> <span class="n">NEGOTIATE_SECURITY_SIGNATURES_ENABLE</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Server supports SMB signing&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sign_options</span> <span class="o">==</span> <span class="n">SMB</span><span class="o">.</span><span class="n">SIGN_WHEN_SUPPORTED</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_signing_active</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;SMB signing activated. All SMB messages will be signed.&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">signing_session_key</span> <span class="o">=</span> <span class="n">signing_session_key</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">capabilities</span> <span class="o">&amp;</span> <span class="n">CAP_EXTENDED_SECURITY</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">signing_challenge_response</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">signing_challenge_response</span> <span class="o">=</span> <span class="n">blob</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;SMB signing deactivated. SMB messages will NOT be signed.&quot;</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">_handleNegotiateResponse_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">uid</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">uid</span>

        <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">hasExtendedSecurity</span> <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">supportsExtendedSecurity</span><span class="p">:</span>
            <span class="n">ntlm_data</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateNegotiateMessage</span><span class="p">()</span>
            <span class="n">blob</span> <span class="o">=</span> <span class="n">securityblob</span><span class="o">.</span><span class="n">generateNegotiateSecurityBlob</span><span class="p">(</span><span class="n">ntlm_data</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComSessionSetupAndxRequest__WithSecurityExtension</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">session_key</span><span class="p">,</span> <span class="n">blob</span><span class="p">)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">nt_password</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">ntlm</span><span class="o">.</span><span class="n">generateChallengeResponseV1</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">,</span> <span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">challenge</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Performing NTLMv1 authentication (without extended security) with challenge &quot;</span><span class="si">%s</span><span class="s1">&quot; and hashed password of &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span><span class="p">,</span>
                          <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">challenge</span><span class="p">),</span>
                          <span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">nt_password</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComSessionSetupAndxRequest__NoSecurityExtension</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">session_key</span><span class="p">,</span>
                                                                                           <span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span>
                                                                                           <span class="n">nt_password</span><span class="p">,</span>
                                                                                           <span class="kc">True</span><span class="p">,</span>
                                                                                           <span class="bp">self</span><span class="o">.</span><span class="n">domain</span><span class="p">)))</span>

    <span class="k">def</span> <span class="nf">_listShares_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;IPC$&#39;</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">connectSrvSvc</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComNTCreateAndxRequest</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">srvsvc&#39;</span><span class="p">,</span>
                                                  <span class="n">flags</span> <span class="o">=</span> <span class="n">NT_CREATE_REQUEST_EXTENDED_RESPONSE</span><span class="p">,</span>
                                                  <span class="n">access_mask</span> <span class="o">=</span> <span class="n">READ_CONTROL</span> <span class="o">|</span> <span class="n">FILE_WRITE_ATTRIBUTES</span> <span class="o">|</span> <span class="n">FILE_READ_ATTRIBUTES</span> <span class="o">|</span> <span class="n">FILE_WRITE_EA</span> <span class="o">|</span> <span class="n">FILE_READ_EA</span> <span class="o">|</span> <span class="n">FILE_APPEND_DATA</span> <span class="o">|</span> <span class="n">FILE_WRITE_DATA</span> <span class="o">|</span> <span class="n">FILE_READ_DATA</span><span class="p">,</span>
                                                  <span class="n">share_access</span> <span class="o">=</span> <span class="n">FILE_SHARE_READ</span> <span class="o">|</span> <span class="n">FILE_SHARE_WRITE</span><span class="p">,</span>
                                                  <span class="n">create_disp</span> <span class="o">=</span> <span class="n">FILE_OPEN</span><span class="p">,</span>
                                                  <span class="n">create_options</span> <span class="o">=</span> <span class="n">FILE_OPEN_NO_RECALL</span> <span class="o">|</span> <span class="n">FILE_NON_DIRECTORY_FILE</span><span class="p">,</span>
                                                  <span class="n">impersonation</span> <span class="o">=</span> <span class="n">SEC_IMPERSONATE</span><span class="p">,</span>
                                                  <span class="n">security_flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectSrvSvcCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">connectSrvSvcCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">call_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getNextRPCCallID</span><span class="p">()</span>
                <span class="c1"># See [MS-CIFS]: 2.2.5.6.1 for more information on TRANS_TRANSACT_NMPIPE (0x0026) parameters</span>
                <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;HH&#39;</span><span class="p">,</span> <span class="mh">0x0026</span><span class="p">,</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
                <span class="c1"># The data_bytes are binding call to Server Service RPC using DCE v1.1 RPC over SMB. See [MS-SRVS] and [C706]</span>
                <span class="c1"># If you wish to understand the meanings of the byte stream, I would suggest you use a recent version of WireShark to packet capture the stream</span>
                <span class="n">data_bytes</span> <span class="o">=</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;05 00 0b 03 10 00 00 00 48 00 00 00&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="o">+</span> \
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">call_id</span><span class="p">)</span> <span class="o">+</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">b8 10 b8 10 00 00 00 00 01 00 00 00 00 00 01 00</span>
<span class="s2">c8 4f 32 4b 70 16 d3 01 12 78 5a 47 bf 6e e1 88</span>
<span class="s2">03 00 00 00 04 5d 88 8a eb 1c c9 11 9f e8 08 00</span>
<span class="s2">2b 10 48 60 02 00 00 00&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTransactionRequest</span><span class="p">(</span><span class="n">max_params_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                     <span class="n">max_data_count</span> <span class="o">=</span> <span class="mi">4280</span><span class="p">,</span>
                                                     <span class="n">max_setup_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                     <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">data_bytes</span><span class="p">,</span>
                                                     <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">setup_bytes</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">create_message</span><span class="o">.</span><span class="n">tid</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">rpcBindCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">create_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to locate Server Service RPC endpoint&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">rpcBindCB</span><span class="p">(</span><span class="n">trans_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">trans_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">trans_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">call_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_getNextRPCCallID</span><span class="p">()</span>

                <span class="n">padding</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>
                <span class="n">server_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="n">server_bytes_len</span> <span class="o">=</span> <span class="n">server_len</span> <span class="o">*</span> <span class="mi">2</span>
                <span class="k">if</span> <span class="n">server_len</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">padding</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\0\0</span><span class="s1">&#39;</span>
                    <span class="n">server_bytes_len</span> <span class="o">+=</span> <span class="mi">2</span>

                <span class="c1"># See [MS-CIFS]: 2.2.5.6.1 for more information on TRANS_TRANSACT_NMPIPE (0x0026) parameters</span>
                <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;HH&#39;</span><span class="p">,</span> <span class="mh">0x0026</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="c1"># The data bytes are the RPC call to NetrShareEnum (Opnum 15) at Server Service RPC.</span>
                <span class="c1"># If you wish to understand the meanings of the byte stream, I would suggest you use a recent version of WireShark to packet capture the stream</span>
                <span class="n">data_bytes</span> <span class="o">=</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;05 00 00 03 10 00 00 00&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="o">+</span> \
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;HHI&#39;</span><span class="p">,</span> <span class="mi">72</span><span class="o">+</span><span class="n">server_bytes_len</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">call_id</span><span class="p">)</span> <span class="o">+</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;4c 00 00 00 00 00 0f 00 00 00 02 00&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="o">+</span> \
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;III&#39;</span><span class="p">,</span> <span class="n">server_len</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">server_len</span><span class="p">)</span> <span class="o">+</span> \
                    <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">padding</span> <span class="o">+</span> \
                    <span class="n">binascii</span><span class="o">.</span><span class="n">unhexlify</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">01 00 00 00 01 00 00 00 04 00 02 00 00 00 00 00</span>
<span class="s2">00 00 00 00 ff ff ff ff 08 00 02 00 00 00 00 00</span>
<span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTransactionRequest</span><span class="p">(</span><span class="n">max_params_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                     <span class="n">max_data_count</span> <span class="o">=</span> <span class="mi">4280</span><span class="p">,</span>
                                                     <span class="n">max_setup_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                     <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">data_bytes</span><span class="p">,</span>
                                                     <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">setup_bytes</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">trans_message</span><span class="o">.</span><span class="n">tid</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">listShareResultsCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">trans_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to bind to Server Service RPC endpoint&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">listShareResultsCB</span><span class="p">(</span><span class="n">result_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">result_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">result_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="c1"># The payload.data_bytes will contain the results of the RPC call to NetrShareEnum (Opnum 15) at Server Service RPC.</span>
                <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">result_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span>

                <span class="k">if</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&amp;</span> <span class="mh">0x02</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">sendReadRequest</span><span class="p">(</span><span class="n">result_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">data_bytes</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">decodeResults</span><span class="p">(</span><span class="n">result_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">data_bytes</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">result_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to retrieve shared device list&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">decodeResults</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">):</span>
            <span class="n">shares_count</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">36</span><span class="p">:</span><span class="mi">40</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>     <span class="c1"># A list of SharedDevice instances</span>
            <span class="n">offset</span> <span class="o">=</span> <span class="mi">36</span> <span class="o">+</span> <span class="mi">12</span>  <span class="c1"># You need to study the byte stream to understand the meaning of these constants</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">shares_count</span><span class="p">):</span>
                <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SharedDevice</span><span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="o">+</span><span class="mi">4</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">8</span><span class="p">])[</span><span class="mi">0</span><span class="p">],</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>
                <span class="n">offset</span> <span class="o">+=</span> <span class="mi">12</span>

            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">shares_count</span><span class="p">):</span>
                <span class="n">max_length</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">length</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;III&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">12</span><span class="p">])</span>
                <span class="n">offset</span> <span class="o">+=</span> <span class="mi">12</span>
                <span class="n">results</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="n">length</span><span class="o">*</span><span class="mi">2</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>

                <span class="k">if</span> <span class="n">length</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span>

                <span class="n">max_length</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">length</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;III&#39;</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">12</span><span class="p">])</span>
                <span class="n">offset</span> <span class="o">+=</span> <span class="mi">12</span>
                <span class="n">results</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">comments</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="n">length</span><span class="o">*</span><span class="mi">2</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>

                <span class="k">if</span> <span class="n">length</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="p">(</span><span class="n">length</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span>

            <span class="n">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">callback</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">sendReadRequest</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">):</span>
            <span class="n">read_count</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">4280</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_raw_size</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComReadAndxRequest</span><span class="p">(</span><span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span>
                                              <span class="n">offset</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">max_return_bytes_count</span> <span class="o">=</span> <span class="n">read_count</span><span class="p">,</span>
                                              <span class="n">min_return_bytes_count</span> <span class="o">=</span> <span class="n">read_count</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">readCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">data_bytes</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">readCB</span><span class="p">(</span><span class="n">read_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">read_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">read_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span>

                <span class="k">if</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&amp;</span> <span class="mh">0x02</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">sendReadRequest</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_bytes&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">24</span><span class="p">:])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">decodeResults</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_bytes&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data_bytes</span><span class="p">[</span><span class="mi">24</span><span class="p">:])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to retrieve shared device list&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComCloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">path</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                <span class="n">connectSrvSvc</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list shares: Unable to connect to IPC$&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">path</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">)</span>
        <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_listPath_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">search</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendFindFirst</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">support_dfs</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;H&#39;</span><span class="p">,</span> <span class="mh">0x0001</span><span class="p">)</span>  <span class="c1"># TRANS2_FIND_FIRST2 sub-command. See [MS-CIFS]: 2.2.6.2.1</span>
            <span class="n">params_bytes</span> <span class="o">=</span> \
                <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;HHHHI&#39;</span><span class="p">,</span>
                            <span class="n">search</span> <span class="o">&amp;</span> <span class="mh">0xFFFF</span><span class="p">,</span> <span class="c1"># SearchAttributes (need to restrict the values due to introduction of SMB_FILE_ATTRIBUTE_INCL_NORMAL)</span>
                            <span class="mi">100</span><span class="p">,</span>    <span class="c1"># SearchCount</span>
                            <span class="mh">0x0006</span><span class="p">,</span> <span class="c1"># Flags: SMB_FIND_CLOSE_AT_EOS | SMB_FIND_RETURN_RESUME_KEYS</span>
                            <span class="mh">0x0104</span><span class="p">,</span> <span class="c1"># InfoLevel: SMB_FIND_FILE_BOTH_DIRECTORY_INFO</span>
                            <span class="mh">0x0000</span><span class="p">)</span> <span class="c1"># SearchStorageType (seems to be ignored by Windows)</span>
            <span class="k">if</span> <span class="n">support_dfs</span><span class="p">:</span>
                <span class="n">params_bytes</span> <span class="o">+=</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\\</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\\</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">service_name</span> <span class="o">+</span> <span class="n">path</span> <span class="o">+</span> <span class="n">pattern</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">params_bytes</span> <span class="o">+=</span> <span class="p">(</span><span class="n">path</span> <span class="o">+</span> <span class="n">pattern</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTransaction2Request</span><span class="p">(</span><span class="n">max_params_count</span> <span class="o">=</span> <span class="mi">10</span><span class="p">,</span>
                                                  <span class="n">max_data_count</span> <span class="o">=</span> <span class="mi">16644</span><span class="p">,</span>
                                                  <span class="n">max_setup_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                  <span class="n">params_bytes</span> <span class="o">=</span> <span class="n">params_bytes</span><span class="p">,</span>
                                                  <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">setup_bytes</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="k">if</span> <span class="n">support_dfs</span><span class="p">:</span>
                <span class="n">m</span><span class="o">.</span><span class="n">flags2</span> <span class="o">|=</span> <span class="n">SMB_FLAGS2_DFS</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">findFirstCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">decodeFindStruct</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">):</span>
            <span class="c1"># SMB_FIND_FILE_BOTH_DIRECTORY_INFO structure. See [MS-CIFS]: 2.2.8.1.7 and [MS-SMB]: 2.2.8.1.1</span>
            <span class="n">info_format</span> <span class="o">=</span> <span class="s1">&#39;&lt;IIQQQQQQIIIBB24s&#39;</span>
            <span class="n">info_size</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">calcsize</span><span class="p">(</span><span class="n">info_format</span><span class="p">)</span>

            <span class="n">data_length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">)</span>
            <span class="n">offset</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="n">offset</span> <span class="o">&lt;</span> <span class="n">data_length</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">offset</span> <span class="o">+</span> <span class="n">info_size</span> <span class="o">&gt;</span> <span class="n">data_length</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:]</span>

                <span class="n">next_offset</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> \
                <span class="n">create_time</span><span class="p">,</span> <span class="n">last_access_time</span><span class="p">,</span> <span class="n">last_write_time</span><span class="p">,</span> <span class="n">last_attr_change_time</span><span class="p">,</span> \
                <span class="n">file_size</span><span class="p">,</span> <span class="n">alloc_size</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="n">filename_length</span><span class="p">,</span> <span class="n">ea_size</span><span class="p">,</span> \
                <span class="n">short_name_length</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">short_name</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="n">info_format</span><span class="p">,</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="n">info_size</span><span class="p">])</span>

                <span class="n">offset2</span> <span class="o">=</span> <span class="n">offset</span> <span class="o">+</span> <span class="n">info_size</span>
                <span class="k">if</span> <span class="n">offset2</span> <span class="o">+</span> <span class="n">filename_length</span> <span class="o">&gt;</span> <span class="n">data_length</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">data_bytes</span><span class="p">[</span><span class="n">offset</span><span class="p">:]</span>

                <span class="n">filename</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">[</span><span class="n">offset2</span><span class="p">:</span><span class="n">offset2</span><span class="o">+</span><span class="n">filename_length</span><span class="p">])</span>
                <span class="n">short_name</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">short_name</span><span class="p">)</span>

                <span class="n">accept_result</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">file_attributes</span> <span class="o">&amp;</span> <span class="mh">0xff</span><span class="p">)</span> <span class="ow">in</span> <span class="p">(</span> <span class="mh">0x00</span><span class="p">,</span> <span class="n">ATTR_NORMAL</span> <span class="p">):</span> <span class="c1"># Only the first 8-bits are compared. We ignore other bits like temp, compressed, encryption, sparse, indexed, etc</span>
                    <span class="n">accept_result</span> <span class="o">=</span> <span class="p">(</span><span class="n">search</span> <span class="o">==</span> <span class="n">SMB_FILE_ATTRIBUTE_NORMAL</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">search</span> <span class="o">&amp;</span> <span class="n">SMB_FILE_ATTRIBUTE_INCL_NORMAL</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">accept_result</span> <span class="o">=</span> <span class="p">(</span><span class="n">file_attributes</span> <span class="o">&amp;</span> <span class="n">search</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>
                <span class="k">if</span> <span class="n">accept_result</span><span class="p">:</span>
                    <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SharedFile</span><span class="p">(</span><span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">create_time</span><span class="p">),</span> <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_access_time</span><span class="p">),</span>
                                              <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_write_time</span><span class="p">),</span> <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_attr_change_time</span><span class="p">),</span>
                                              <span class="n">file_size</span><span class="p">,</span> <span class="n">alloc_size</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="n">short_name</span><span class="p">,</span> <span class="n">filename</span><span class="p">))</span>

                <span class="k">if</span> <span class="n">next_offset</span><span class="p">:</span>
                    <span class="n">offset</span> <span class="o">+=</span> <span class="n">next_offset</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">break</span>
            <span class="k">return</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>

        <span class="k">def</span> <span class="nf">findFirstCB</span><span class="p">(</span><span class="n">find_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">find_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">find_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="k">if</span> <span class="s1">&#39;total_count&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                    <span class="c1"># TRANS2_FIND_FIRST2 response. [MS-CIFS]: 2.2.6.2.2</span>
                    <span class="n">sid</span><span class="p">,</span> <span class="n">search_count</span><span class="p">,</span> <span class="n">end_of_search</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">last_name_offset</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;HHHHH&#39;</span><span class="p">,</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">params_bytes</span><span class="p">[:</span><span class="mi">10</span><span class="p">])</span>
                    <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span> <span class="s1">&#39;sid&#39;</span><span class="p">:</span> <span class="n">sid</span><span class="p">,</span> <span class="s1">&#39;end_of_search&#39;</span><span class="p">:</span> <span class="n">end_of_search</span><span class="p">,</span> <span class="s1">&#39;last_name_offset&#39;</span><span class="p">:</span> <span class="n">last_name_offset</span><span class="p">,</span> <span class="s1">&#39;data_buf&#39;</span><span class="p">:</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span> <span class="p">})</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sid</span><span class="p">,</span> <span class="n">end_of_search</span><span class="p">,</span> <span class="n">last_name_offset</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;sid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;end_of_search&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;last_name_offset&#39;</span><span class="p">]</span>

                <span class="n">send_next</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">if</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">:</span>
                    <span class="n">d</span> <span class="o">=</span> <span class="n">decodeFindStruct</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_buf&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">)</span>
                    <span class="k">if</span> <span class="s1">&#39;data_count&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">)</span> <span class="o">!=</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">total_data_count</span><span class="p">:</span>
                            <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span> <span class="s1">&#39;data_count&#39;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">),</span>
                                            <span class="s1">&#39;total_count&#39;</span><span class="p">:</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">total_data_count</span><span class="p">,</span>
                                            <span class="s1">&#39;data_buf&#39;</span><span class="p">:</span> <span class="n">d</span><span class="p">,</span>
                                            <span class="p">})</span>
                            <span class="n">send_next</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_count&#39;</span><span class="p">]</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">)</span>
                        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;total_count&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">total_data_count</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;total_count&#39;</span><span class="p">])</span>
                        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_buf&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span>
                        <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_count&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;total_count&#39;</span><span class="p">]:</span>
                            <span class="n">send_next</span> <span class="o">=</span> <span class="kc">False</span>

                <span class="k">if</span> <span class="ow">not</span> <span class="n">send_next</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">find_message</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">findFirstCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">end_of_search</span><span class="p">:</span>
                    <span class="n">callback</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sendFindNext</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">sid</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">results</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;support_dfs&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">find_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xC000000F</span><span class="p">:</span>  <span class="c1"># [MS-ERREF]: STATUS_NO_SUCH_FILE</span>
                    <span class="c1"># Remote server returns STATUS_NO_SUCH_FILE error so we assume that the search returns no matching files</span>
                    <span class="n">callback</span><span class="p">([</span> <span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to retrieve file list&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendFindNext</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">sid</span><span class="p">,</span> <span class="n">resume_key</span><span class="p">,</span> <span class="n">resume_file</span><span class="p">,</span> <span class="n">support_dfs</span><span class="p">):</span>
            <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;H&#39;</span><span class="p">,</span> <span class="mh">0x0002</span><span class="p">)</span>  <span class="c1"># TRANS2_FIND_NEXT2 sub-command. See [MS-CIFS]: 2.2.6.3.1</span>
            <span class="n">params_bytes</span> <span class="o">=</span> \
                <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;HHHIH&#39;</span><span class="p">,</span>
                            <span class="n">sid</span><span class="p">,</span>        <span class="c1"># SID</span>
                            <span class="mi">100</span><span class="p">,</span>        <span class="c1"># SearchCount</span>
                            <span class="mh">0x0104</span><span class="p">,</span>     <span class="c1"># InfoLevel: SMB_FIND_FILE_BOTH_DIRECTORY_INFO</span>
                            <span class="n">resume_key</span><span class="p">,</span> <span class="c1"># ResumeKey</span>
                            <span class="mh">0x0006</span><span class="p">)</span>     <span class="c1"># Flags: SMB_FIND_RETURN_RESUME_KEYS | SMB_FIND_CLOSE_AT_EOS</span>
            <span class="n">params_bytes</span> <span class="o">+=</span> <span class="p">(</span><span class="n">resume_file</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTransaction2Request</span><span class="p">(</span><span class="n">max_params_count</span> <span class="o">=</span> <span class="mi">10</span><span class="p">,</span>
                                                  <span class="n">max_data_count</span> <span class="o">=</span> <span class="mi">16644</span><span class="p">,</span>
                                                  <span class="n">max_setup_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                  <span class="n">params_bytes</span> <span class="o">=</span> <span class="n">params_bytes</span><span class="p">,</span>
                                                  <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">setup_bytes</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="k">if</span> <span class="n">support_dfs</span><span class="p">:</span>
                <span class="n">m</span><span class="o">.</span><span class="n">flags2</span> <span class="o">|=</span> <span class="n">SMB_FLAGS2_DFS</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">findNextCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">sid</span> <span class="o">=</span> <span class="n">sid</span><span class="p">,</span> <span class="n">support_dfs</span> <span class="o">=</span> <span class="n">support_dfs</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">findNextCB</span><span class="p">(</span><span class="n">find_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">find_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">find_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="k">if</span> <span class="s1">&#39;total_count&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                    <span class="c1"># TRANS2_FIND_NEXT2 response. [MS-CIFS]: 2.2.6.3.2</span>
                    <span class="n">search_count</span><span class="p">,</span> <span class="n">end_of_search</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">last_name_offset</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;HHHH&#39;</span><span class="p">,</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">params_bytes</span><span class="p">[:</span><span class="mi">8</span><span class="p">])</span>
                    <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span> <span class="s1">&#39;end_of_search&#39;</span><span class="p">:</span> <span class="n">end_of_search</span><span class="p">,</span> <span class="s1">&#39;last_name_offset&#39;</span><span class="p">:</span> <span class="n">last_name_offset</span><span class="p">,</span> <span class="s1">&#39;data_buf&#39;</span><span class="p">:</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span> <span class="p">})</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">end_of_search</span><span class="p">,</span> <span class="n">last_name_offset</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;end_of_search&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;last_name_offset&#39;</span><span class="p">]</span>

                <span class="n">send_next</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">if</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">:</span>
                    <span class="n">d</span> <span class="o">=</span> <span class="n">decodeFindStruct</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_buf&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">)</span>
                    <span class="k">if</span> <span class="s1">&#39;data_count&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">)</span> <span class="o">!=</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">total_data_count</span><span class="p">:</span>
                            <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span> <span class="s1">&#39;data_count&#39;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">),</span>
                                            <span class="s1">&#39;total_count&#39;</span><span class="p">:</span> <span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">total_data_count</span><span class="p">,</span>
                                            <span class="s1">&#39;data_buf&#39;</span><span class="p">:</span> <span class="n">d</span><span class="p">,</span>
                                            <span class="p">})</span>
                            <span class="n">send_next</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_count&#39;</span><span class="p">]</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">)</span>
                        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;total_count&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">total_data_count</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;total_count&#39;</span><span class="p">])</span>
                        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_buf&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span>
                        <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;data_count&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;total_count&#39;</span><span class="p">]:</span>
                            <span class="n">send_next</span> <span class="o">=</span> <span class="kc">False</span>

                <span class="k">if</span> <span class="ow">not</span> <span class="n">send_next</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">find_message</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">findNextCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">end_of_search</span><span class="p">:</span>
                    <span class="n">callback</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sendFindNext</span><span class="p">(</span><span class="n">find_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;sid&#39;</span><span class="p">],</span> <span class="mi">0</span><span class="p">,</span> <span class="n">results</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;support_dfs&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to retrieve file list&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendDFSReferral</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;H&#39;</span><span class="p">,</span> <span class="mh">0x0010</span><span class="p">)</span>  <span class="c1"># TRANS2_GET_DFS_REFERRAL sub-command. See [MS-CIFS]: 2.2.6.16.1</span>
            <span class="n">params_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;H&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>      <span class="c1"># Max referral level 3</span>
            <span class="n">params_bytes</span> <span class="o">+=</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\\</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\\</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">service_name</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTransaction2Request</span><span class="p">(</span><span class="n">max_params_count</span> <span class="o">=</span> <span class="mi">10</span><span class="p">,</span>
                                                  <span class="n">max_data_count</span> <span class="o">=</span> <span class="mi">16644</span><span class="p">,</span>
                                                  <span class="n">max_setup_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                  <span class="n">params_bytes</span> <span class="o">=</span> <span class="n">params_bytes</span><span class="p">,</span>
                                                  <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">setup_bytes</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">dfsReferralCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">dfsReferralCB</span><span class="p">(</span><span class="n">dfs_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">sendFindFirst</span><span class="p">(</span><span class="n">dfs_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="k">if</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">optional_support</span> <span class="o">&amp;</span> <span class="n">SMB_TREE_CONNECTX_SUPPORT_DFS</span><span class="p">:</span>
                        <span class="n">sendDFSReferral</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">sendFindFirst</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendFindFirst</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_getAttributes_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendQuery</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;H&#39;</span><span class="p">,</span> <span class="mh">0x0005</span><span class="p">)</span>  <span class="c1"># TRANS2_QUERY_PATH_INFORMATION sub-command. See [MS-CIFS]: 2.2.6.6.1</span>
            <span class="n">params_bytes</span> <span class="o">=</span> \
                <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;HI&#39;</span><span class="p">,</span>
                            <span class="mh">0x0107</span><span class="p">,</span> <span class="c1"># SMB_QUERY_FILE_ALL_INFO ([MS-CIFS] 2.2.2.3.3)</span>
                            <span class="mh">0x0000</span><span class="p">)</span> <span class="c1"># Reserved</span>
            <span class="n">params_bytes</span> <span class="o">+=</span> <span class="p">(</span><span class="n">path</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;UTF-16LE&#39;</span><span class="p">)</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTransaction2Request</span><span class="p">(</span><span class="n">max_params_count</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span>
                                                  <span class="n">max_data_count</span> <span class="o">=</span> <span class="mi">65535</span><span class="p">,</span>
                                                  <span class="n">max_setup_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                  <span class="n">params_bytes</span> <span class="o">=</span> <span class="n">params_bytes</span><span class="p">,</span>
                                                  <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">setup_bytes</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">queryCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">queryCB</span><span class="p">(</span><span class="n">query_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">query_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">query_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">info_format</span> <span class="o">=</span> <span class="s1">&#39;&lt;QQQQIIQQ&#39;</span>
                <span class="n">info_size</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">calcsize</span><span class="p">(</span><span class="n">info_format</span><span class="p">)</span>
                <span class="n">create_time</span><span class="p">,</span> <span class="n">last_access_time</span><span class="p">,</span> <span class="n">last_write_time</span><span class="p">,</span> <span class="n">last_attr_change_time</span><span class="p">,</span> \
                <span class="n">file_attributes</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">alloc_size</span><span class="p">,</span> <span class="n">file_size</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="n">info_format</span><span class="p">,</span> <span class="n">query_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">[:</span><span class="n">info_size</span><span class="p">])</span>
                <span class="n">filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extractLastPathComponent</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

                <span class="n">info</span> <span class="o">=</span> <span class="n">SharedFile</span><span class="p">(</span><span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">create_time</span><span class="p">),</span> <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_access_time</span><span class="p">),</span> <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_write_time</span><span class="p">),</span> <span class="n">convertFILETIMEtoEpoch</span><span class="p">(</span><span class="n">last_attr_change_time</span><span class="p">),</span>
                                  <span class="n">file_size</span><span class="p">,</span> <span class="n">alloc_size</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get attributes for </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Read failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendQuery</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to get attributes for </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendQuery</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_getSecurity_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path_file_pattern</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;getSecurity is not yet implemented for SMB1&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_retrieveFile_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_retrieveFileFromOffset</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_retrieveFileFromOffset_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">starting_offset</span><span class="p">,</span> <span class="n">max_length</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendOpen</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComOpenAndxRequest</span><span class="p">(</span><span class="n">filename</span> <span class="o">=</span> <span class="n">path</span><span class="p">,</span>
                                              <span class="n">access_mode</span> <span class="o">=</span> <span class="mh">0x0040</span><span class="p">,</span>  <span class="c1"># Sharing mode: Deny nothing to others</span>
                                              <span class="n">open_mode</span> <span class="o">=</span> <span class="mh">0x0001</span><span class="p">,</span>    <span class="c1"># Failed if file does not exist</span>
                                              <span class="n">search_attributes</span> <span class="o">=</span> <span class="n">SMB_FILE_ATTRIBUTE_HIDDEN</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_SYSTEM</span><span class="p">,</span>
                                              <span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">openCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">openCB</span><span class="p">(</span><span class="n">open_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">open_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">max_length</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">closeFid</span><span class="p">(</span><span class="n">open_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
                    <span class="n">callback</span><span class="p">((</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">file_attributes</span><span class="p">,</span> <span class="mi">0</span> <span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sendRead</span><span class="p">(</span><span class="n">open_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span> <span class="n">starting_offset</span><span class="p">,</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">file_attributes</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">max_length</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to retrieve </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendRead</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="n">read_len</span><span class="p">,</span> <span class="n">remaining_len</span><span class="p">):</span>
            <span class="n">read_count</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_raw_size</span> <span class="o">-</span> <span class="mi">2</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComReadAndxRequest</span><span class="p">(</span><span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span>
                                              <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="p">,</span>
                                              <span class="n">max_return_bytes_count</span> <span class="o">=</span> <span class="n">read_count</span><span class="p">,</span>
                                              <span class="n">min_return_bytes_count</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mh">0xFFFF</span><span class="p">,</span> <span class="n">read_count</span><span class="p">)))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">readCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="p">,</span> <span class="n">file_attributes</span> <span class="o">=</span> <span class="n">file_attributes</span><span class="p">,</span>
                                                           <span class="n">read_len</span> <span class="o">=</span> <span class="n">read_len</span><span class="p">,</span> <span class="n">remaining_len</span> <span class="o">=</span> <span class="n">remaining_len</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">readCB</span><span class="p">(</span><span class="n">read_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="c1"># To avoid crazy memory usage when retrieving large files, we do not save every read_message in messages_history.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">read_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">read_len</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;read_len&#39;</span><span class="p">]</span>
                <span class="n">remaining_len</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;remaining_len&#39;</span><span class="p">]</span>
                <span class="n">data_len</span> <span class="o">=</span> <span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_length</span>
                <span class="k">if</span> <span class="n">max_length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">data_len</span> <span class="o">&gt;</span> <span class="n">remaining_len</span><span class="p">:</span>
                        <span class="n">file_obj</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">[:</span><span class="n">remaining_len</span><span class="p">])</span>
                        <span class="n">read_len</span> <span class="o">+=</span> <span class="n">remaining_len</span>
                        <span class="n">remaining_len</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">file_obj</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
                        <span class="n">remaining_len</span> <span class="o">-=</span> <span class="n">data_len</span>
                        <span class="n">read_len</span> <span class="o">+=</span> <span class="n">data_len</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">file_obj</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
                    <span class="n">read_len</span> <span class="o">+=</span> <span class="n">data_len</span>

                <span class="k">if</span> <span class="p">(</span><span class="n">max_length</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">remaining_len</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="ow">or</span> <span class="n">data_len</span> <span class="o">&lt;</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_raw_size</span> <span class="o">-</span> <span class="mi">2</span><span class="p">):</span>
                    <span class="n">closeFid</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                    <span class="n">callback</span><span class="p">((</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;file_attributes&#39;</span><span class="p">],</span> <span class="n">read_len</span> <span class="p">))</span>  <span class="c1"># Note that this is a tuple of 3-elements</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sendRead</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">]</span><span class="o">+</span><span class="n">data_len</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;file_attributes&#39;</span><span class="p">],</span> <span class="n">read_len</span><span class="p">,</span> <span class="n">remaining_len</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">read_message</span><span class="p">)</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">read_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to retrieve </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Read failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComCloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendOpen</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to retrieve </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendOpen</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_storeFile_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_storeFileFromOffset_SMB1</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_storeFileFromOffset_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">starting_offset</span><span class="p">,</span> <span class="n">truncate</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendOpen</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComOpenAndxRequest</span><span class="p">(</span><span class="n">filename</span> <span class="o">=</span> <span class="n">path</span><span class="p">,</span>
                                              <span class="n">access_mode</span> <span class="o">=</span> <span class="mh">0x0041</span><span class="p">,</span>  <span class="c1"># Sharing mode: Deny nothing to others + Open for writing</span>
                                              <span class="n">open_mode</span> <span class="o">=</span> <span class="mh">0x0012</span> <span class="k">if</span> <span class="n">truncate</span> <span class="k">else</span> <span class="mh">0x0011</span><span class="p">,</span>    <span class="c1"># Create file if file does not exist. Overwrite or append depending on truncate parameter.</span>
                                              <span class="n">search_attributes</span> <span class="o">=</span> <span class="n">SMB_FILE_ATTRIBUTE_HIDDEN</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_SYSTEM</span><span class="p">,</span>
                                              <span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">openCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">openCB</span><span class="p">(</span><span class="n">open_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">open_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">sendWrite</span><span class="p">(</span><span class="n">open_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">,</span> <span class="n">starting_offset</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to store </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open file&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendWrite</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span><span class="p">):</span>
            <span class="c1"># [MS-SMB] 2.2.4.5.2.2: The total SMB message size (inclusive of SMB header) must be not exceed the max_buffer_size.</span>
            <span class="n">write_count</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_buffer_size</span><span class="o">-</span><span class="mi">64</span><span class="p">,</span> <span class="mh">0xFFFF</span><span class="o">-</span><span class="mi">64</span><span class="p">)</span> <span class="c1"># SMB header is 32-bytes. We factor in another 32-bytes for the message parameter block</span>
            <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">file_obj</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">write_count</span><span class="p">)</span>
            <span class="n">data_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_bytes</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">data_len</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComWriteAndxRequest</span><span class="p">(</span><span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="p">,</span> <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">data_bytes</span><span class="p">))</span>
                <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">writeCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">,</span> <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="o">+</span><span class="n">data_len</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">)</span>
                <span class="n">callback</span><span class="p">((</span> <span class="n">file_obj</span><span class="p">,</span> <span class="n">offset</span> <span class="p">))</span>  <span class="c1"># Note that this is a tuple of 2-elements</span>

        <span class="k">def</span> <span class="nf">writeCB</span><span class="p">(</span><span class="n">write_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="c1"># To avoid crazy memory usage when saving large files, we do not save every write_message in messages_history.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">write_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">sendWrite</span><span class="p">(</span><span class="n">write_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">write_message</span><span class="p">)</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">write_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to store </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Write failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComCloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendOpen</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to store </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendOpen</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_deleteFiles_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path_file_pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">pattern</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path_file_pattern</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">path_components</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;*&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">or</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;?&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path_components</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">pattern</span> <span class="o">=</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">messages_history</span><span class="p">,</span> <span class="n">files_queue</span> <span class="o">=</span> <span class="p">[</span> <span class="p">],</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">if</span> <span class="n">pattern</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">path_components</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">path_components</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">files_queue</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path_components</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">path_components</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">files_queue</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">path</span> <span class="p">))</span>

        <span class="k">def</span> <span class="nf">deleteCB</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">files_queue</span><span class="p">:</span>
                <span class="n">p</span><span class="p">,</span> <span class="n">filename</span> <span class="o">=</span> <span class="n">files_queue</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">filename</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">p</span><span class="p">:</span>
                        <span class="n">filename</span> <span class="o">=</span> <span class="n">p</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">filename</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB1__del</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">],</span> <span class="n">filename</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_deleteDirectory_SMB1</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path_file_pattern</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">listCB</span><span class="p">(</span><span class="n">files_list</span><span class="p">):</span>
            <span class="n">files_queue</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">files_list</span><span class="p">)</span>
            <span class="n">deleteCB</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="k">if</span> <span class="n">files_queue</span><span class="p">:</span>
                        <span class="n">deleteCB</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB1__list</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">files_queue</span><span class="p">:</span>
                <span class="n">deleteCB</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_deleteFiles_SMB1__list</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_deleteFiles_SMB1__list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">delete_matching_folders</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="n">folder_queue</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">files_list</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">current_path</span> <span class="o">=</span> <span class="p">[</span> <span class="n">path</span> <span class="p">]</span>
        <span class="n">search</span> <span class="o">=</span> <span class="n">SMB_FILE_ATTRIBUTE_READONLY</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_HIDDEN</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_SYSTEM</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_DIRECTORY</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_ARCHIVE</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_INCL_NORMAL</span>

        <span class="k">def</span> <span class="nf">listCB</span><span class="p">(</span><span class="n">results</span><span class="p">):</span>
            <span class="n">files</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">filename</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span> <span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="s1">&#39;..&#39;</span> <span class="p">],</span> <span class="n">results</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">isDirectory</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">delete_matching_folders</span><span class="p">:</span>
                        <span class="n">folder_queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="o">+</span><span class="n">f</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">files</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">f</span><span class="o">.</span><span class="n">filename</span> <span class="p">))</span>
            <span class="k">if</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">!=</span><span class="n">path</span> <span class="ow">and</span> <span class="n">delete_matching_folders</span><span class="p">:</span>
                <span class="n">files</span><span class="o">.</span><span class="n">append</span><span class="p">((</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="kc">None</span> <span class="p">))</span>

            <span class="k">if</span> <span class="n">files</span><span class="p">:</span>
                <span class="n">files_list</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">files</span>

            <span class="k">if</span> <span class="n">folder_queue</span><span class="p">:</span>
                <span class="n">p</span> <span class="o">=</span> <span class="n">folder_queue</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
                <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">p</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_listPath_SMB1</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">current_path</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">search</span> <span class="o">=</span> <span class="n">search</span><span class="p">,</span> <span class="n">pattern</span> <span class="o">=</span> <span class="s1">&#39;*&#39;</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">files_list</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_listPath_SMB1</span><span class="p">(</span><span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">listCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">search</span> <span class="o">=</span> <span class="n">search</span><span class="p">,</span> <span class="n">pattern</span> <span class="o">=</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_deleteFiles_SMB1__del</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">tid</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendDelete</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComDeleteRequest</span><span class="p">(</span><span class="n">filename_pattern</span> <span class="o">=</span> <span class="n">path</span><span class="p">,</span>
                                            <span class="n">search_attributes</span> <span class="o">=</span> <span class="n">SMB_FILE_ATTRIBUTE_HIDDEN</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_SYSTEM</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">deleteCB</span><span class="p">(</span><span class="n">delete_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">delete_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xC000000F</span><span class="p">:</span> <span class="c1"># [MS-ERREF]: STATUS_NO_SUCH_FILE</span>
                <span class="c1"># If there are no matching files, we just treat as success instead of failing</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path_file_pattern</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xC00000BA</span><span class="p">:</span> <span class="c1"># [MS-ERREF]: STATUS_FILE_IS_A_DIRECTORY</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Cannot delete a folder. Please use deleteDirectory() method or append &quot;/*&quot; to your path if you wish to delete all files in the folder.&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>
            <span class="k">elif</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">internal_value</span> <span class="o">==</span> <span class="mh">0xC0000034</span><span class="p">:</span> <span class="c1"># [MS-ERREF]: STATUS_OBJECT_NAME_INVALID</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Path not found&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Delete failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="n">sendDelete</span><span class="p">(</span><span class="n">tid</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_resetFileAttributes_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path_file_pattern</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">file_attributes</span><span class="o">=</span><span class="n">ATTR_NORMAL</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;resetFileAttributes is not yet implemented for SMB1&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_createDirectory_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendCreate</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComCreateDirectoryRequest</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">createCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">createCB</span><span class="p">(</span><span class="n">create_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">create_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to create directory </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Create failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendCreate</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to create directory </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendCreate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_deleteDirectory_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendDelete</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComDeleteDirectoryRequest</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">deleteCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">deleteCB</span><span class="p">(</span><span class="n">delete_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">delete_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">delete_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete directory </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Delete failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendDelete</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to delete directory </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendDelete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_rename_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">new_path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">new_path</span> <span class="o">=</span> <span class="n">new_path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">old_path</span> <span class="o">=</span> <span class="n">old_path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendRename</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComRenameRequest</span><span class="p">(</span><span class="n">old_path</span> <span class="o">=</span> <span class="n">old_path</span><span class="p">,</span>
                                            <span class="n">new_path</span> <span class="o">=</span> <span class="n">new_path</span><span class="p">,</span>
                                            <span class="n">search_attributes</span> <span class="o">=</span> <span class="n">SMB_FILE_ATTRIBUTE_HIDDEN</span> <span class="o">|</span> <span class="n">SMB_FILE_ATTRIBUTE_SYSTEM</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">renameCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">renameCB</span><span class="p">(</span><span class="n">rename_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rename_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">rename_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">((</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">new_path</span> <span class="p">))</span>  <span class="c1"># Note that this is a tuple of 2-elements</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to rename </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Rename failed&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendRename</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to rename </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">old_path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendRename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_listSnapshots_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">service_name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_authenticated</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotReadyError</span><span class="p">(</span><span class="s1">&#39;SMB connection not authenticated&#39;</span><span class="p">)</span>

        <span class="n">expiry_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">sendOpen</span><span class="p">(</span><span class="n">tid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComOpenAndxRequest</span><span class="p">(</span><span class="n">filename</span> <span class="o">=</span> <span class="n">path</span><span class="p">,</span>
                                              <span class="n">access_mode</span> <span class="o">=</span> <span class="mh">0x0040</span><span class="p">,</span>  <span class="c1"># Sharing mode: Deny nothing to others</span>
                                              <span class="n">open_mode</span> <span class="o">=</span> <span class="mh">0x0001</span><span class="p">,</span>    <span class="c1"># Failed if file does not exist</span>
                                              <span class="n">search_attributes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                              <span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">openCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">openCB</span><span class="p">(</span><span class="n">open_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">open_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">open_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">sendEnumSnapshots</span><span class="p">(</span><span class="n">open_message</span><span class="o">.</span><span class="n">tid</span><span class="p">,</span> <span class="n">open_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">fid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list snapshots </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to open path&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">sendEnumSnapshots</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="c1"># [MS-CIFS]: 2.2.7.2</span>
            <span class="c1"># [MS-SMB]: 2.2.7.2.1</span>
            <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;IHBB&#39;</span><span class="p">,</span>
                                      <span class="mh">0x00144064</span><span class="p">,</span>  <span class="c1"># [MS-SMB]: 2.2.7.2.1</span>
                                      <span class="n">fid</span><span class="p">,</span>         <span class="c1"># FID</span>
                                      <span class="mh">0x01</span><span class="p">,</span>        <span class="c1"># IsFctl</span>
                                      <span class="mi">0</span><span class="p">)</span>           <span class="c1"># IsFlags</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComNTTransactRequest</span><span class="p">(</span><span class="n">function</span> <span class="o">=</span> <span class="mh">0x0002</span><span class="p">,</span>  <span class="c1"># NT_TRANSACT_IOCTL. [MS-CIFS]: 2.2.7.2.1</span>
                                                <span class="n">max_params_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                <span class="n">max_data_count</span> <span class="o">=</span> <span class="mh">0xFFFF</span><span class="p">,</span>
                                                <span class="n">max_setup_count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
                                                <span class="n">setup_bytes</span> <span class="o">=</span> <span class="n">setup_bytes</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">enumSnapshotsCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span><span class="p">,</span> <span class="n">fid</span> <span class="o">=</span> <span class="n">fid</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">enumSnapshotsCB</span><span class="p">(</span><span class="n">enum_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">enum_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">enum_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">results</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>
                <span class="n">snapshots_count</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;I&#39;</span><span class="p">,</span> <span class="n">enum_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">8</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">snapshots_count</span><span class="p">):</span>
                    <span class="n">s</span> <span class="o">=</span> <span class="n">DataFaultToleranceStrategy</span><span class="o">.</span><span class="n">data_bytes_decode</span><span class="p">(</span><span class="n">enum_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data_bytes</span><span class="p">[</span><span class="mi">12</span><span class="o">+</span><span class="n">i</span><span class="o">*</span><span class="mi">50</span><span class="p">:</span><span class="mi">12</span><span class="o">+</span><span class="mi">48</span><span class="o">+</span><span class="n">i</span><span class="o">*</span><span class="mi">50</span><span class="p">])</span>
                    <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="o">*</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="p">(</span> <span class="n">s</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">9</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">10</span><span class="p">:</span><span class="mi">12</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">13</span><span class="p">:</span><span class="mi">15</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">16</span><span class="p">:</span><span class="mi">18</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">19</span><span class="p">:</span><span class="mi">21</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">22</span><span class="p">:</span><span class="mi">24</span><span class="p">]</span> <span class="p">)))))</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">closeFid</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;tid&#39;</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fid&#39;</span><span class="p">])</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list snapshots </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to list snapshots on path&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">closeFid</span><span class="p">(</span><span class="n">tid</span><span class="p">,</span> <span class="n">fid</span><span class="p">):</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComCloseRequest</span><span class="p">(</span><span class="n">fid</span><span class="p">))</span>
            <span class="n">m</span><span class="o">.</span><span class="n">tid</span> <span class="o">=</span> <span class="n">tid</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">service_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">connectCB</span><span class="p">(</span><span class="n">connect_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">connect_message</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span>
                    <span class="n">sendOpen</span><span class="p">(</span><span class="n">connect_message</span><span class="o">.</span><span class="n">tid</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Failed to list snapshots </span><span class="si">%s</span><span class="s1"> on </span><span class="si">%s</span><span class="s1">: Unable to connect to shared device&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">messages_history</span><span class="p">))</span>

            <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComTreeConnectAndxRequest</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;</span><span class="se">\\</span><span class="si">%s</span><span class="s1">\</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_name</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">service_name</span> <span class="p">),</span> <span class="n">SERVICE_ANY</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">connectCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">service_name</span><span class="p">)</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sendOpen</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connected_trees</span><span class="p">[</span><span class="n">service_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_echo_SMB1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">30</span><span class="p">):</span>
        <span class="n">messages_history</span> <span class="o">=</span> <span class="p">[</span> <span class="p">]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Echo data must be </span><span class="si">%s</span><span class="s1"> not </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">echoCB</span><span class="p">(</span><span class="n">echo_message</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">echo_message</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">echo_message</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">hasError</span><span class="p">:</span>
                <span class="n">callback</span><span class="p">(</span><span class="n">echo_message</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">errback</span><span class="p">(</span><span class="n">OperationFailure</span><span class="p">(</span><span class="s1">&#39;Echo failed&#39;</span><span class="p">,</span> <span class="n">messages_history</span><span class="p">))</span>

        <span class="n">m</span> <span class="o">=</span> <span class="n">SMBMessage</span><span class="p">(</span><span class="n">ComEchoRequest</span><span class="p">(</span><span class="n">echo_data</span> <span class="o">=</span> <span class="n">data</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sendSMBMessage</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pending_requests</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">]</span> <span class="o">=</span> <span class="n">_PendingRequest</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">mid</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">+</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">echoCB</span><span class="p">,</span> <span class="n">errback</span><span class="p">)</span>
        <span class="n">messages_history</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_extractLastPathComponent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>


<div class="viewcode-block" id="SharedDevice"><a class="viewcode-back" href="../../api/smb_SharedDevice.html#smb.base.SharedDevice">[docs]</a><span class="k">class</span> <span class="nc">SharedDevice</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Contains information about a single shared device on the remote server.</span>

<span class="sd">    The following attributes are available:</span>

<span class="sd">    * name : An unicode string containing the name of the shared device</span>
<span class="sd">    * comments : An unicode string containing the user description of the shared device</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># The following constants are taken from [MS-SRVS]: 2.2.2.4</span>
    <span class="c1"># They are used to identify the type of shared resource from the results from the NetrShareEnum in Server Service RPC</span>
    <span class="n">DISK_TREE</span>   <span class="o">=</span> <span class="mh">0x00</span>
    <span class="n">PRINT_QUEUE</span> <span class="o">=</span> <span class="mh">0x01</span>
    <span class="n">COMM_DEVICE</span> <span class="o">=</span> <span class="mh">0x02</span>
    <span class="n">IPC</span>         <span class="o">=</span> <span class="mh">0x03</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">comments</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">=</span> <span class="nb">type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>         <span class="c1">#: An unicode string containing the name of the shared device</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">comments</span> <span class="o">=</span> <span class="n">comments</span> <span class="c1">#: An unicode string containing the user description of the shared device</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">type</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns one of the following integral constants.</span>
<span class="sd">         - SharedDevice.DISK_TREE</span>
<span class="sd">         - SharedDevice.PRINT_QUEUE</span>
<span class="sd">         - SharedDevice.COMM_DEVICE</span>
<span class="sd">         - SharedDevice.IPC</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">&amp;</span> <span class="mh">0xFFFF</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">isSpecial</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if this shared device is a special share reserved for interprocess communication (IPC$)</span>
<span class="sd">        or remote administration of the server (ADMIN$). Can also refer to administrative shares such as</span>
<span class="sd">        C$, D$, E$, and so forth</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">&amp;</span> <span class="mh">0x80000000</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">isTemporary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if this is a temporary share that is not persisted for creation each time the file server initializes.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">&amp;</span> <span class="mh">0x40000000</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;Shared device: </span><span class="si">%s</span><span class="s1"> (type:0x</span><span class="si">%02x</span><span class="s1"> comments:</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">comments</span> <span class="p">)</span></div>


<div class="viewcode-block" id="SharedFile"><a class="viewcode-back" href="../../api/smb_SharedFile.html#smb.base.SharedFile">[docs]</a><span class="k">class</span> <span class="nc">SharedFile</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Contain information about a file/folder entry that is shared on the shared device.</span>

<span class="sd">    As an application developer, you should not need to instantiate a *SharedFile* instance directly in your application.</span>
<span class="sd">    These *SharedFile* instances are usually returned via a call to *listPath* method in :doc:`smb.SMBProtocol.SMBProtocolFactory&lt;smb_SMBProtocolFactory&gt;`.</span>

<span class="sd">    If you encounter *SharedFile* instance where its short_name attribute is empty but the filename attribute contains a short name which does not correspond</span>
<span class="sd">    to any files/folders on your remote shared device, it could be that the original filename on the file/folder entry on the shared device contains</span>
<span class="sd">    one of these prohibited characters: &quot;\\/[]:+|&lt;&gt;=;?,* (see [MS-CIFS]: 2.2.1.1.1 for more details).</span>

<span class="sd">    The following attributes are available:</span>

<span class="sd">    * create_time : Float value in number of seconds since 1970-01-01 00:00:00 to the time of creation of this file resource on the remote server</span>
<span class="sd">    * last_access_time : Float value in number of seconds since 1970-01-01 00:00:00 to the time of last access of this file resource on the remote server</span>
<span class="sd">    * last_write_time : Float value in number of seconds since 1970-01-01 00:00:00 to the time of last modification of this file resource on the remote server</span>
<span class="sd">    * last_attr_change_time : Float value in number of seconds since 1970-01-01 00:00:00 to the time of last attribute change of this file resource on the remote server</span>
<span class="sd">    * file_size : File size in number of bytes</span>
<span class="sd">    * alloc_size : Total number of bytes allocated to store this file</span>
<span class="sd">    * file_attributes : A SMB_EXT_FILE_ATTR integer value. See [MS-CIFS]: 2.2.1.2.3. You can perform bit-wise tests to determine the status of the file using the ATTR_xxx constants in smb_constants.py.</span>
<span class="sd">    * short_name : Unicode string containing the short name of this file (usually in 8.3 notation)</span>
<span class="sd">    * filename : Unicode string containing the long filename of this file. Each OS has a limit to the length of this file name. On Windows, it is 256 characters.</span>
<span class="sd">    * file_id : Long value representing the file reference number for the file. If the remote system does not support this field, this field will be None or 0. See [MS-FSCC]: 2.4.17</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">create_time</span><span class="p">,</span> <span class="n">last_access_time</span><span class="p">,</span> <span class="n">last_write_time</span><span class="p">,</span> <span class="n">last_attr_change_time</span><span class="p">,</span> <span class="n">file_size</span><span class="p">,</span> <span class="n">alloc_size</span><span class="p">,</span> <span class="n">file_attributes</span><span class="p">,</span> <span class="n">short_name</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">file_id</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">create_time</span> <span class="o">=</span> <span class="n">create_time</span>  <span class="c1">#: Float value in number of seconds since 1970-01-01 00:00:00 to the time of creation of this file resource on the remote server</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">last_access_time</span> <span class="o">=</span> <span class="n">last_access_time</span>  <span class="c1">#: Float value in number of seconds since 1970-01-01 00:00:00 to the time of last access of this file resource on the remote server</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">last_write_time</span> <span class="o">=</span> <span class="n">last_write_time</span>    <span class="c1">#: Float value in number of seconds since 1970-01-01 00:00:00 to the time of last modification of this file resource on the remote server</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">last_attr_change_time</span> <span class="o">=</span> <span class="n">last_attr_change_time</span>  <span class="c1">#: Float value in number of seconds since 1970-01-01 00:00:00 to the time of last attribute change of this file resource on the remote server</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file_size</span> <span class="o">=</span> <span class="n">file_size</span>   <span class="c1">#: File size in number of bytes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alloc_size</span> <span class="o">=</span> <span class="n">alloc_size</span> <span class="c1">#: Total number of bytes allocated to store this file</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file_attributes</span> <span class="o">=</span> <span class="n">file_attributes</span> <span class="c1">#: A SMB_EXT_FILE_ATTR integer value. See [MS-CIFS]: 2.2.1.2.3. You can perform bit-wise tests to determine the status of the file using the ATTR_xxx constants in smb_constants.py.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">short_name</span> <span class="o">=</span> <span class="n">short_name</span> <span class="c1">#: Unicode string containing the short name of this file (usually in 8.3 notation)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>     <span class="c1">#: Unicode string containing the long filename of this file. Each OS has a limit to the length of this file name. On Windows, it is 256 characters.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file_id</span> <span class="o">=</span> <span class="n">file_id</span>       <span class="c1">#: Long value representing the file reference number for the file. If the remote system does not support this field, this field will be None or 0. See [MS-FSCC]: 2.4.17</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">isDirectory</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;A convenience property to return True if this file resource is a directory on the remote server&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_attributes</span> <span class="o">&amp;</span> <span class="n">ATTR_DIRECTORY</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">isReadOnly</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;A convenient property to return True if this file resource is read-only on the remote server&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_attributes</span> <span class="o">&amp;</span> <span class="n">ATTR_READONLY</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">isNormal</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A convenient property to return True if this is a normal file.</span>

<span class="sd">        Note that pysmb defines a normal file as a file entry that is not read-only, not hidden, not system, not archive and not a directory.</span>
<span class="sd">        It ignores other attributes like compression, indexed, sparse, temporary and encryption.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_attributes</span><span class="o">==</span><span class="n">ATTR_NORMAL</span><span class="p">)</span> <span class="ow">or</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">file_attributes</span> <span class="o">&amp;</span> <span class="mh">0xff</span><span class="p">)</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;Shared file: </span><span class="si">%s</span><span class="s1"> (FileSize:</span><span class="si">%d</span><span class="s1"> bytes, isDirectory:</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">file_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">isDirectory</span> <span class="p">)</span></div>


<span class="k">class</span> <span class="nc">_PendingRequest</span><span class="p">:</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mid</span><span class="p">,</span> <span class="n">expiry_time</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">errback</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mid</span> <span class="o">=</span> <span class="n">mid</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">expiry_time</span> <span class="o">=</span> <span class="n">expiry_time</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">callback</span> <span class="o">=</span> <span class="n">callback</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">errback</span> <span class="o">=</span> <span class="n">errback</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span> <span class="o">=</span> <span class="n">kwargs</span>
</pre></div>

            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">pysmb 1.2.8 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >Module code</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">smb.base</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2001-2021, Michael Teo https://miketeo.net/.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.0.1.
    </div>
  </body>
</html>